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

Insert method should return a result instead of void

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 4.0.0
    • Affects Version/s: 3.0.0
    • Component/s: API
    • None
    • Major Change

      In the Java driver, MongoCollection.insertOne seems to behave inconsistently with insert behavior in Mongo Shell and the pyMongo insert_one method. The Java method is void whereas the collection.insert(Document) method in Mongo Shell returns the result as an object (typeof reports 'object'). In pyMongo, insert_one returns a pymongo.results.InsertOneResult object.

      Furthermore, the Java Driver mutates the parameter passed to insertOne() by adding the "_id" key. This does not happen to the document passed to insert() in the Mongo Shell and I don't think it happens to the document that is passed to insert_one in pyMongo either.

      At any rate, the behavior of the Java driver with respect to the results of insertOne violates the Principle of Least Astonishment, IMO, and at the very least, the JavaDocs should be updated to at least warn developers of this inconsistent behavior.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jlacar Junilu Lacar
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: