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

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

    Posting this as this might be helpful for someone. You can find the updated object like this:

    await req.db
        .collection('users')
        .insertOne({ email, password: hashedPassword, name  })
        .then(({ ops }) => ops[0]);
    

提交回复
热议问题