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

后端 未结 6 1343
谎友^
谎友^ 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:42

    You could use mongoose to do this. With the save method you can insert a document and return it on success. Here is an example from the mongoose documentation:

    product.save(function (err, product, numAffected) {
      if (err) {
        // Handle error...
      } else {
        // Do something with the returned document...
      }
    })
    

提交回复
热议问题