mongodb从数组中拉出所有元素 [英] mongodb pull all element from the array

查看:67
本文介绍了mongodb从数组中拉出所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下架构:

"_id" : 1,
  "n" : [{
      "a" : ObjectId("4ef0ca414653b7c866040000"),
      "d" : new Date("Thu, 22 Dec 2011 04:53:56 GMT +04:00")
    }, {
      "a" : ObjectId("4ef0ca414653b9c866040000"),
      "d" : new Date("Thu, 22 Dec 2011 04:54:11 GMT +04:00")
    }, {
      "a" : ObjectId("4ef0ca424653b9c866040000"),
      "d" : new Date("Thu, 22 Dec 2011 04:54:30 GMT +04:00"),
   }]

我需要删除所有n,其中d小于特定日期.

and I need to remove all n, where d is less than specific date.

所以我想我可以通过以下方式做到这一点:

So I thought I will be able to do this in the following way:

db.coll.update({
'_id': 1
},{
 $pullAll : {
  n.d : {
     $lte : new Date(2000, 10, 11)
   }
 }
})

但是问题是,它不能以这种方式工作. 有什么建议吗?

but the problem is, that it is not working this way. Any suggestions?

推荐答案

这不是$ pullAll的工作原理.您不能指定匹配条件,只能指定要删除的对象数组(需要完全匹配).

That is not how $pullAll works. You cannot specify a matching condition, you can only specify an array of objects to be deleted (that need to match exactly).

幸运的是,您可以改用$ pull(它确实接受匹配条件):

Fortunately, you can use $pull instead (which does accept a matching condition):

db.coll.update({
'_id': 1
},{
 $pull : {
  n.d : {
     $lte : new Date(2000, 10, 11)
   }
 }
})

请注意,$ pull还会拉出匹配的 all 个元素,而不仅仅是一个.

Note that $pull also pulls all elements that match, not just one.

这是令人困惑的地方.

这篇关于mongodb从数组中拉出所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆