无法使用 $pullAll

unable to use $pullAll

我有一个集合,其中包含如下所示的文档

{ 
    "_id" : ObjectId("56d92901f9d573cc1c1fb8bb"), 
    "busEntryExitInformation" : {
        "dateTime" : ISODate("2016-03-04T06:19:45.914+0000"), 
        "busEntryExitEvent" : [
            {
                "plateNumber" : "ADFN3R2", 
                "direction" : "EXIT", 
                "routeNumber" : NumberInt(929), 
                "driverID" : "DId5", 
                "driverName" : "john", 
                "_id" : ObjectId("56d92901f9d573cc1c1fb8c0")
            }, 
            {
                "plateNumber" : "ADFN3R4", 
                "direction" : "EXIT", 
                "routeNumber" : NumberInt(652), 
                "driverID" : "DId2", 
                "driverName" : "jack", 
                "_id" : ObjectId("56d92901f9d573cc1c1fb8bf")
            }, 
            {
                "plateNumber" : "ADFN3R8", 
                "direction" : "EXIT", 
                "routeNumber" : NumberInt(500), 
                "driverID" : "DId5", 
                "driverName" : "john", 
                "_id" : ObjectId("56d92901f9d573cc1c1fb8be")
            }, 
            {
                "plateNumber" : "ADFN3R7", 
                "direction" : "ENTRY", 
                "routeNumber" : NumberInt(500), 
                "driverID" : "DId3", 
                "driverName" : "mack", 
                "_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
            }, 
            {
                "plateNumber" : "ADFN3R2", 
                "direction" : "EXIT", 
                "routeNumber" : NumberInt(652), 
                "driverID" : "DId2", 
                "driverName" : "sandesh", 
                "_id" : ObjectId("56d92901f9d573cc1c1fb8bc")
            }
        ], 
        "cameraIntrinsics" : {
            "cameraFocalLength" : NumberInt(35), 
            "cameraAngle" : NumberInt(20), 
            "imageWidth" : "1920", 
            "imageHeight" : "1080", 
            "frameRate" : NumberInt(25)
        }, 
        "cameraExtrinsics" : {
            "cameraId" : NumberInt(1), 
            "cameraName" : "Under Route-090 NorthboundBridge", 
            "cameraDirection" : "Towards Northbound Lanes", 
            "cameraLatitude" : 1.350228, 
            "cameraLongitude" : 103.984889, 
            "cameraHeight" : NumberInt(30)
        }
    }, 
    "__v" : NumberInt(0)
}
}

其中 busEntryExitEvent 是一个数组,我正在尝试删除集合中所有文档的方向退出的数组元素 结果应包含所有带有方向条目的文档

  { 
        "_id" : ObjectId("56d92901f9d573cc1c1fb8bb"), 
        "busEntryExitInformation" : {
            "dateTime" : ISODate("2016-03-04T06:19:45.914+0000"), 
            "busEntryExitEvent" : [

                {
                    "plateNumber" : "ADFN3R7", 
                    "direction" : "ENTRY", 
                    "routeNumber" : NumberInt(500), 
                    "driverID" : "DId3", 
                    "driverName" : "mack", 
                    "_id" : ObjectId("56d92901f9d573cc1c1fb8bd")
                }
            ], 
            "cameraIntrinsics" : {
                "cameraFocalLength" : NumberInt(35), 
                "cameraAngle" : NumberInt(20), 
                "imageWidth" : "1920", 
                "imageHeight" : "1080", 
                "frameRate" : NumberInt(25)
            }, 
            "cameraExtrinsics" : {
                "cameraId" : NumberInt(1), 
                "cameraName" : "Under Route-090 NorthboundBridge", 
                "cameraDirection" : "Towards Northbound Lanes", 
                "cameraLatitude" : 1.350228, 
                "cameraLongitude" : 103.984889, 
                "cameraHeight" : NumberInt(30)
            }
        }, 
        "__v" : NumberInt(0)
    }
    }  

我正在尝试这样做

db.busEntryExitDoc.update(
      {  $pull: { busEntryExitEvent: {  "direction" : "EXIT"} } },
      { multi: false }
      )

必须对所有文档执行此操作此处没有 where 条件如何操作请帮助

你做错了 .update() 的第一个参数是 "query" 参数,这里应该是一个空文档。

db.busEntryExitDoc.update( 
    {},
    {  $pull: { "busEntryExitInformation.busEntryExitEvent": { "direction": "EXIT" }  } },
    {  multi: true }
)

同样的事情适用于 .updateMany() 方法

db.busEntryExitDoc.update( 
    {},
    {  $pull: { "busEntryExitInformation.busEntryExitEvent":  { "direction": "EXIT" }  } },
)

MongoDB update() 方法需要三个参数:db.collection.update(query, update, options)。如果要对所有文档执行 update,只需使用一个空对象作为 query:

db.busEntryExitDoc.update({},
    { $pull: { 'busEntryExitInformation.busEntryExitEvent' : {'direction' : "EXIT"} } },
    { multi: true }
);

另外,请注意 dot notation to access the array field: you have to point the $pull operator to an array otherwise the operation fails. Finally, use { multi: true } as options 的使用,因为您要更新集合中的所有文档。