How to insert a doc into mongodb using mongoose and get the generated id?

前端 未结 4 1385
渐次进展
渐次进展 2020-12-16 10:06

I\'m using mongoose to operate mongodb. Now, for testing, I want to inserting some data into mongodb by native connection.

But the question is how to get the generat

4条回答
  •  忘掉有多难
    2020-12-16 10:34

    If you use .save then you'll get the _id back in the callback function.

    var user = new User({
      a: 'abc'
    });
    
    user.save(function (err, results) {
      console.log(results._id);
    });
    

提交回复
热议问题