Get the _id of inserted document in Mongo database in NodeJS

前端 未结 10 1984
半阙折子戏
半阙折子戏 2020-11-27 02:53

I use NodeJS to insert documents in MongoDB. Using collection.insert I can insert a document into database like in this code:

// ...
collection.         


        
10条回答
  •  萌比男神i
    2020-11-27 03:43

    Mongo sends the complete document as a callbackobject so you can simply get it from there only.

    for example

    collection.save(function(err,room){
      var newRoomId = room._id;
      });
    

提交回复
热议问题