Announcement Announcement Module
Collapse
No announcement yet.
MongoDB unvailable "upsert" update Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB unvailable "upsert" update

    The class MongoTemplate has a protected method doUpdate (...), in it's signature is a boolean parameter upsert. This method is called from methods updateFirst () and updateMulti() with always set to upsert = false. Thus there is no possibility to upsert. Maybe there are other ways?

  • #2
    +1 I'd also like to see a way to upsert. Maybe methods in mongoTemplate named upsertFirst and upsertMulti?

    Comment


    • #3
      Would you mind simply opening up a JIRA ticket if you'd like to see that supported?

      Comment


      • #4
        Jira Ticket

        Done! https://jira.springsource.org/browse/DATAMONGO-308

        Comment


        • #5
          The work-around I am using until this is changed.

          By overriding the prepareWriteConcern method, I am ensuring that the doUpdate method follows the code path that will use the collection.update(queryObj, updateObj, upsert, multi, writeConcernToUse) method call.

          Then I just added my own upsertMethods.

          Code:
          <bean id="mongoTemplate" class="com.han.mongo.HanMongoTemplate">
          	<constructor-arg ref="mongoDbFactory" />
          	<constructor-arg name="mongoConverter" ref="mappingConverter" />
          </bean>
          Code:
          package com.han.mongo;
          
          import org.springframework.data.mongodb.MongoDbFactory;
          import org.springframework.data.mongodb.core.MongoTemplate;
          import org.springframework.data.mongodb.core.convert.MongoConverter;
          import org.springframework.data.mongodb.core.query.Query;
          import org.springframework.data.mongodb.core.query.Update;
          
          import com.mongodb.WriteConcern;
          import com.mongodb.WriteResult;
          
          public class HanMongoTemplate extends MongoTemplate {
          	public HanMongoTemplate(MongoDbFactory mongoDbFactory,
          			MongoConverter mongoConverter) {
          		super(mongoDbFactory, mongoConverter);
          	}
          
          	@Override
          	protected WriteConcern prepareWriteConcern(WriteConcern writeConcern) {
          		return super.prepareWriteConcern(writeConcern != null ? writeConcern : WriteConcern.NORMAL);
          	}
          
          	public WriteResult upsertFirst(Query query, Update update, Class<?> entityClass) {
          		return doUpdate(getCollectionName(entityClass), query, update, entityClass, true, false);
          	}
          
          	public WriteResult upsertFirst(final Query query, final Update update, final String collectionName) {
          		return doUpdate(collectionName, query, update, null, true, false);
          	}
          
          	public WriteResult upsertMulti(Query query, Update update, Class<?> entityClass) {
          		return doUpdate(getCollectionName(entityClass), query, update, entityClass, true, true);
          	}
          
          	public WriteResult upsertMulti(final Query query, final Update update, String collectionName) {
          		return doUpdate(collectionName, query, update, null, true, true);
          	}
          }

          Comment

          Working...
          X