Node.js + MongoDB: insert one and return the newly inserted document

后端 未结 6 1339
谎友^
谎友^ 2021-01-01 12:15

I\'m wondering if there is a way to insert new document and return it in one go.

This is what I\'m currently using:

db.collection(\'mycollection\').i         


        
6条回答
  •  时光取名叫无心
    2021-01-01 12:32

    The following code worked for me, in MongoDB version 2.2.33.

    db.collection("sample_collection").insertOne({
       field1: "abcde"
    }, (err, result) => {
       if(err) console.log(err);
       else console.log(result.ops[0].field1)
    }
    

提交回复
热议问题