MongoDB更新查询以根据值取消设置特定的数组成员

时间:2016-05-14 09:16:01

标签: node.js mongodb mongodb-update

我有一个子文档数组,如下所示


    /* record 1 */
        {
                        "_id" : "462044",
                        "program" : [ 
                                {
                                    "name" : "Business Services"
                                }, 
                                {
                                    "name" : "Homeland Security"
                                }, 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Engineering"
                                }, 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Computer Software"
                                }
                                ]

            }

    /* record 2 */
            {
                        "_id" : "462045",
                        "program" : [ 
                                {
                                    "name" : "Business Services"
                                }, 
                                {
                                    "name" : "Homeland Security"
                                }, 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Engineering"
                                }, 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Computer Software"
                                }
                                ]

            }      

[我想删除没有'标题'子构件]

如何编写mongo update query或nodejs函数以获取以下文档集


    /* record 1 */
        {

                        "_id" : "462044",
                        "program" : [ 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Engineering"
                                }, 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Computer Software"
                                }
                                ]

        }


    /* record 2 */
        {
                        "_id" : "462045",
                        "program [
                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Engineering"
                                }, 

                                {
                                    "title" : [ 
                                        "associate"
                                    ],
                                    "name" : "Computer Software"
                                }
                                ]

        }      

1 个答案:

答案 0 :(得分:0)

嗯,首先你的文档结构看起来很奇怪。在您的案例school1school2中,每条记录都有一个不同的字段代表基本相同的信息。

如果您将这些字段重命名为school,则可以轻松修复。

如果您修复了文档结构,则可以借助聚合框架和纯更新查询轻松删除所需的子文档。

示例:

db.collection.aggregate([
  {
    $project: {
      school: {
        $filter: {
          input: "$school.program",
          as: "prog",
          cond: {
            $ne: [
              {
                $ifNull: [
                  "$$prog.title",
                  true
                ]
              },
              true
            ]
          }
        }
      }
    }
  }
]).forEach(function(o){
  db.collection.updateOne({
    _id: o._id
  },
  {
    $set: {
      school: o.school
    }
  });
})

这会将您的旧文档转换为(删除所有不包含title的程序的子字段:

{ 
    "_id" : ObjectId("57371cdf81e9959ba1a5fab0"), 
    "school" : [
        {
            "title" : [
                "associate"
            ], 
            "name" : "Engineering"
        }, 
        {
            "title" : [
                "associate"
            ], 
            "name" : "Computer Software"
        }
    ]
}
{ 
    "_id" : ObjectId("57371cdf81e9959ba1a5fab1"), 
    "school" : [
        {
            "title" : [
                "associate"
            ], 
            "name" : "Engineering"
        }, 
        {
            "title" : [
                "associate"
            ], 
            "name" : "Computer Software"
        }
    ]
}