Results 1 to 3 of 3

Thread: mongoTemplate update by ID not work?

  1. #1
    Join Date
    Dec 2007
    Location
    Boston, MA
    Posts
    34

    Default mongoTemplate update by ID not work?

    I'm using Spring Mongo 1.0.0M2. I tried to update a property in a document and it failed when query by ID. This is the mongo template configuration:
    Code:
    @Override
    public Mongo mongo() throws Exception {
    	ServerAddress address = new ServerAddress( "127.0.0.1" , 27017);
    		
    	MongoOptions options = new MongoOptions();
    	options.autoConnectRetry = true;
    	options.connectionsPerHost=10;
    	options.threadsAllowedToBlockForConnectionMultiplier=5;
    	options.maxWaitTime=6000;
    	options.connectTimeout=0;
    	options.socketTimeout=0;
    		
    	Mongo mongo = new Mongo(address, options);
    	return mongo;
    }
    
    @Override
    public MongoTemplate mongoTemplate() throws Exception {
    	MongoTemplate mongoTemplate = new MongoTemplate(mongo(), "ecl");
            mongoTemplate.setMongoConverter(mappingMongoConverter());
    	mongoTemplate.setUsername(USERNAME);
    	mongoTemplate.setPassword(PASSWORD);
    	return mongoTemplate;
    }
    	
    //	public String getMappingBasePackage() { 
    //	    return "dist.bo";
    //	}
    The injected MongoOperations works for save document, update entire document. It also works fine when:
    Code:
    mongoTemplate.updateFirst("entityCollectionName", 
            query(where("name").is("object name")), 
    	update("deleted", true));
    but never work if the query is
    Code:
            query(whereId().is(the_document_id)),
    The funny thing is, retrieving document by id with such query works fine:
    Code:
    mongoTemplate.findOne(query(whereId().is(the_document_id)), 
    	MyDoc.class);
    I never found any tutorial/reference talking about update a property by query ID.

    Did I miss something?
    Last edited by leewill; Apr 21st, 2011 at 10:49 AM.

  2. #2
    Join Date
    Dec 2007
    Location
    Boston, MA
    Posts
    34

    Unhappy

    it turned out not just me who got this problem:
    http://forum.springsource.org/showthread.php?t=107358

  3. #3
    Join Date
    Dec 2007
    Location
    Boston, MA
    Posts
    34

    Red face solved

    solved the problem. You can't use whereId().is(....). Here's my test codes:

    Code:
    Query q = query(whereId().is(new ObjectId(entity.getId())));
    logger.info("query: " + q.getQueryObject());
    		
    WriteResult result = mongoTemplate.updateFirst(entityCollectionName, 
    	q,
    	update("deleted", true));
    This one will fail, and the query string in "q" is:
    { "id" : { "$oid" : "4db0718de0f46a0e2828532d"}}

    But if change the whereId() to where("_id"), i.e.,
    Code:
    Query q = query(where("_id").is(new ObjectId(entity.getId())));
    It works, and the query string in "q" is:
    { "_id" : { "$oid" : "4db0718de0f46a0e2828532d"}}

    It seems whereId() in update has some issue, as it works perfect in save(), find()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •