Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-233 CRUD and Interfaces Documentation Chapter
  3. DOCS-468

Mention that insert adds the ObjectId to the object passed in

    XMLWordPrintableJSON

Details

    Description

      I was looking for a way to retrieve the ObjectID of the objects being inserted until I found out that it is added automatically to the object I passed to the insert method. This totally makes sense now that I think about it given that the ObjectId is calculated on the client side.I didn't find this mentioned anywhere so I think it should be added to the documentation.

      I'm not sure this is working with all the drivers but it does at least for the Java one as this little program shows:
      DBCollection testCollection = testDB.getCollection("names");
      BasicDBObject newName = new BasicDBObject("name", "joe");
      testCollection.insert(newName);
      System.out.println(newName);

      { "name" : "joe" , "_id" : { "$oid" : "50402b3a744e0a126a98fe0e"}}

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            gforget Gui Forget
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              11 years, 18 weeks, 5 days ago