Javascript 无法使用$pull(Mongoose)删除用户模型数组中的对象
我已经尝试了各种方法和方法,我正在尝试使用$pull方法 这是我的用户对象 我以前在用户对象中只有对象id,但是现在我正在删除一个完整的对象,我搜索发布的id,然后尝试将其拉出Javascript 无法使用$pull(Mongoose)删除用户模型数组中的对象,javascript,reactjs,database,mongodb,mongoose,Javascript,Reactjs,Database,Mongodb,Mongoose,我已经尝试了各种方法和方法,我正在尝试使用$pull方法 这是我的用户对象 我以前在用户对象中只有对象id,但是现在我正在删除一个完整的对象,我搜索发布的id,然后尝试将其拉出 router.delete('/testing', (req, res, next) => { postModel.findByIdAndRemove(req.query.postid, (error, returnedDocuments) => { if (error) r
router.delete('/testing', (req, res, next) => {
postModel.findByIdAndRemove(req.query.postid, (error, returnedDocuments) => {
if (error) return next(error);
userModel.findOneAndUpdate(
// { email: req.query.email, posts: req.query.postid },
{ email: req.query.email },
{ $pull: { posts: { number: mongoose.Types.ObjectId(req.query.postid) } } },
{ new: true },
function(error, user) {
if (error) {
res.json(error);
}
console.log(`Post id ===== ${req.query.postid}`);
console.log(`Email===== ${req.query.email}`);
console.log(`returning user====${user}`);
res.json('Successfully updated user');
}
);
});
});
正在从postmodel中删除帖子,但我无法从users postings Make数组中删除该对象
如能提供任何其他解决方案或见解,了解为何会发生这种情况,我们将不胜感激 问题在于MongoDB在值之前比较类型,并且没有应用隐式类型转换,因此没有:
{ $pull: { posts: { number: req.query.postid } } }
您需要运行:
{ $pull: { posts: { number: mongoose.Types.ObjectId(req.query.postid) } } }
问题是MongoDB在值之前比较类型,并且没有应用隐式类型转换,因此没有:
{ $pull: { posts: { number: req.query.postid } } }
您需要运行:
{ $pull: { posts: { number: mongoose.Types.ObjectId(req.query.postid) } } }
{$pull:{posts:{number:req.query.postid}}
==>在posts
数组中是否有名为number
的属性。因为你提供的样品中没有。对不起,这太奇怪了。。。。我再次复制了它。我还尝试了{$pull:{“posts.$.number”:req.query.postid}},
但没有cookie:(对于posts.$.number,我得到一个错误:“errmsg”:“位置运算符没有从查询中找到所需的匹配项。”,
可能是因为我需要输入对象或什么?@jcx3x是req.query.postid
字符串或ObjectId
?{$pull:{posts:{number:req.query.postid}
==>在posts
数组中是否有一个名为number
的属性。因为它不在您提供的示例中。很抱歉,这太奇怪了……我再次复制了它。我还尝试了{$pull:{“posts.$.number”:req.query.postid},
但没有cookie:(对于posts.$.number,我得到一个错误:“errmsg”:“位置运算符没有从查询中找到所需的匹配项。”,
可能是因为我需要输入对象或什么?@jcx3x是req.query.postid
字符串或ObjectId
?