对 $pullAll 在 MongoDB 中如何工作感到困惑
我有一个如下所示的文档:
db.blog.findOne()
{
"_id" : ObjectId("4dc1c938c4bfb4d21a000001"),
"blogid" : 1,
"body" : "Lorem ipsum dolor",
"comments" : [
{
"id" : 1,
"name" : "Alex",
"comment" : "Test",
"approved" : 1
},
{
"id" : 2,
"name" : "Phil",
"comment" : "Test",
"approved" : 1
},
{
"id" : 3,
"name" : "Joe",
"comment" : "Test",
"approved" : 0
}
],
"no_comments" : 11,
"title" : "Hello world"
}
如果我运行查询
db.blog.update({'blogid':1}, { $pull : { 'comments' : {'approved' : 0} } });
那么它将删除第三条评论。
相反,如果我想提取已批准为 0 或 1 的所有评论,则以下查询不起作用:
db.blog.update({'blogid':1}, { $pullAll : { 'comments' : {'approved' : [0,1]} } });
我收到错误
允许修饰符 $pushAll/pullAll 仅数组
有人可以解释我哪里出错了吗?
谢谢
I've got a document that looks like this:
db.blog.findOne()
{
"_id" : ObjectId("4dc1c938c4bfb4d21a000001"),
"blogid" : 1,
"body" : "Lorem ipsum dolor",
"comments" : [
{
"id" : 1,
"name" : "Alex",
"comment" : "Test",
"approved" : 1
},
{
"id" : 2,
"name" : "Phil",
"comment" : "Test",
"approved" : 1
},
{
"id" : 3,
"name" : "Joe",
"comment" : "Test",
"approved" : 0
}
],
"no_comments" : 11,
"title" : "Hello world"
}
If I run the query
db.blog.update({'blogid':1}, { $pull : { 'comments' : {'approved' : 0} } });
Then it will remove the third comment.
If instead I want to pull all comments where approved is 0 or 1 the following query doesn't work:
db.blog.update({'blogid':1}, { $pullAll : { 'comments' : {'approved' : [0,1]} } });
I get the error
Modifier $pushAll/pullAll allowed for
arrays only
Can someone please explain where I'm going wrong?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$pullAll
需要完全匹配。您可以使用$pull
代替:$pullAll
requires an exact match. You might use$pull
instead:这是因为 $pullAll 接受数组,而不是对象。我想以下代码应该有效:
This is because $pullAll takes array, not an object. I guess follwing code should work:
$pull update 运算符:
$pullAll 更新运算符:
$pull update operator:
$pullAll update operator: