MongoDB的更新查询基于所述值未设置特定阵列成员

问题描述:

我有子文档的阵列像下面MongoDB的更新查询基于所述值未设置特定阵列成员

 

    /* 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" 
           } 
           ] 

      }  

[我想删除不具有“标题”的子构件阵列成员]

我怎么能写蒙戈更新查询或函数的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" 
           } 
           ] 

     }  

那么,首先所有的文档结构看起来很奇怪。每个记录都有一个不同的字段,表示基本上相同的信息,在您的案例中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" 
     } 
    ] 
}