Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2284

The updateOne command with upsert option does not work if document does not exist already.

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.3.0
    • Component/s: Async
    • Labels:
      None

      The updateOne command below works with MongoDB Java driver 3.3.0 but does not work with MongoDB Async Java driver 3.3.0.

      MongoAsync Driver does not work:

      	public void upsertForReal(MongoCollection<Document> mCollection, Document query, Document upsert) {
      		mCollection.updateOne(query, upsert, new UpdateOptions().upsert(true), new SingleResultCallback<UpdateResult>() {
      			@Override
      			public void onResult(UpdateResult result, Throwable e) {
      				if (e!=null) {
      					log.error("Upsert failed", e);	
      				}
      				if (log.isDebugEnabled()) {
      					log.debug("The upsert query "+query+" returned matchedCount="+result.getMatchedCount()+ " modifiedCount= "+result.getModifiedCount());
      				}
      									
      			}
      		});
      	}
      

      Mongo driver works:

      	public void upsertForReal(MongoCollection<Document> mCollection, Document query, Document upsert) {
      		mCollection.updateOne(query, upsert, new UpdateOptions().upsert(true));
      	}
      

      The MongoAsync driver does not report any issues. The document simply does not get inserted if the query document does not match.

            Assignee:
            Unassigned Unassigned
            Reporter:
            tareqnabeel Tareq Nabeel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: