Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在数组中查找特定文档_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 在数组中查找特定文档

Node.js 在数组中查找特定文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,因此,我有一个应用程序,它有一组用户,并在一个数组中存储每个特定用户的注释: 如果我有便笺的id和便笺所属的用户,我如何查询该特定便笺并($pull/delete)它。 我已尝试为其找到正确的查询,但无效: app.get('/delete/:userId/:noteId', (req,res)=>{ let noteId = req.params.noteId; User.findOne({'notes': {_id: noteId}},(err,foundNote)=>{

因此,我有一个应用程序,它有一组用户,并在一个数组中存储每个特定用户的注释:

如果我有便笺的id和便笺所属的用户,我如何查询该特定便笺并($pull/delete)它。 我已尝试为其找到正确的查询,但无效:

app.get('/delete/:userId/:noteId', (req,res)=>{
let noteId = req.params.noteId;
User.findOne({'notes': {_id: noteId}},(err,foundNote)=>{
    console.log(foundNote);
});
}))


我要找的是一个我需要设置的条件,以便找到特定的note文档。提前感谢:)。

使用ObjectId,您的过滤器将是:

{'notes.\u id':ObjectId(noteId)}

您可以使用$pull:

app.get('/delete/:userId/:noteId', (req,res)=>{
   let noteId = req.params.noteId;
   let userId= req.params.userId;
   User.findByIdAndUpdate(userId, { $pull: { notes: noteId  })
})