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.

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 3.3.0
    • Async
    • None

    Description

      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.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: