Node.js 在数组中查找特定文档
因此,我有一个应用程序,它有一组用户,并在一个数组中存储每个特定用户的注释: 如果我有便笺的id和便笺所属的用户,我如何查询该特定便笺并($pull/delete)它。 我已尝试为其找到正确的查询,但无效: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)=>{
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 })
})