如何删除 MongoDB 数组中缺少字段的元素?

How to remove elements with missing field in a MongoDB array?

最近自学MongoDB和Python遇到了这个问题

https://i.stack.imgur.com/acQxl.png

object 1-3 中有 date 信息,但某些对象不包含日期。如何使用 MongoDB 和 Python 脚本删除其中的 object that doesn't contain date?提前致谢。

使用 $pull 从数组中删除项目。

$pull

查找 update 的一部分:在 date 字段上使用 $exists 来检查是否存在。

db.coll.update({},
{
  "$pull": {
    "arr": {
      "a": {
        exists: false
      }
    }
  }
})

试试

update(
{} ,
{ "$pull": { data :{ date: { "$exists":  false } }} },
false,true)