1

when I run the simple code below to insert a new document:

collectionUsers.insert({'name':'john'},function(err,records){
    if (err) throw err;
    console.log("Id of new document added =  " + records[0]._id);
});

I get the following errors:

catch(err) { process.nextTick(function() { throw err}); }
                                             ^
TypeError: Cannot read property '_id' of undefined

Doesn't the callback function return an array? I'm just trying to get the id of the new document and save it in a variable for future use. Thanks for your help.

Alternatively, if I use insertOne instead, I get this output: Id of new document added = {"ok":1,"n":1}

But I want to use insert...

cmonkey
  • 13
  • 1
  • 5
  • As [the documentation states](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#~insertWriteOpCallback), the callback gets called with a [`insertWriteOpResultObject`](http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#~insertWriteOpResult) object. – robertklep Jan 01 '16 at 08:09

2 Answers2

8

There is ops object in records which contains inserted doc/docs.

Try:

collectionUsers.insert({'name':'john'},function(err,records){
   // You can explore more here
   console.log("record contents",JSON.stringify(records,null,4));
   // Desired output
   console.log("Id of new document added =  " + records.ops[0]._id);
});
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
  • When I add you line I get this output: record contents [object Object]...why doesn't records just give me the document? I have seen many examples with the same code and they don't return any errors. – cmonkey Jan 01 '16 at 07:55
  • Answer edited. Its `npm mongodb` library implementation which give you this results. alternatively you can use `npm mongoose` which is wrapper over mongodb npm. It will give you desired results, more info [here](http://stackoverflow.com/questions/10520501/how-to-insert-a-doc-into-mongodb-using-mongoose-and-get-the-generated-id) – Jagdish Idhate Jan 01 '16 at 08:06
  • With your stringify code I get this (same as if I were to use insertOne): { "ok": 1, "n": 1 } – cmonkey Jan 01 '16 at 08:08
  • @user2285490 `mongoose` is much more than just a wrapper. It's a great library, but perhaps a bit overkill just to get the desired result :-) The inserted document _is_ available through `records.ops`, as you suggest. – robertklep Jan 01 '16 at 08:08
  • sorry guys...so why cant records access the name field? – cmonkey Jan 01 '16 at 08:11
  • which `mongodb` version you are using? the latest version `2.1.2` gives all results `record contents { "result": { "ok": 1, "n": 1 }, "ops": [ { "name": "john", "_id": "56863232030094bc0da3017b" } ], "insertedCount": 1, "insertedIds": [ "56863232030094bc0da3017b" ] } Id of new document added = 56863232030094bc0da3017b` – Jagdish Idhate Jan 01 '16 at 08:17
0

Thanks a lot user2285490 and robertklep. I finally realized what the ops object in record is. This is the api documentation for future reference:

http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#~insertWriteOpCallback

cmonkey
  • 13
  • 1
  • 5