Get the _id of inserted document in Mongo database in NodeJS

前端 未结 10 2005
半阙折子戏
半阙折子戏 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条回答
  •  没有蜡笔的小新
    2020-11-27 03:50

    There is a second parameter for the callback for collection.insert that will return the doc or docs inserted, which should have _ids.

    Try:

    collection.insert(objectToInsert, function(err,docsInserted){
        console.log(docsInserted);
    });
    

    and check the console to see what I mean.

提交回复
热议问题