如何根据mongodb中的对象属性删除数组中的对象?

时间:2013-02-22 12:20:30

标签: node.js mongodb

这是我的mongodb文件。

"_id" : ObjectId("5123731f2763c9682a000001"),
"created_on" : ISODate("2013-02-19T12:42:07.835Z"),
"currentLogin" : ISODate("2013-02-22T06:03:25.603Z"),
"email" : "xxx@test.com",
"extraPhotos" : 0,
"reqmail" : [ ],
"isActivated" : true,
"lastLogin" : ISODate("2013-02-20T05:42:15.359Z"),
"linkedAccounts" : [
    {
        "birthday" : "11/01/1984",
        "email" : "testingxxx234@gmail.com",
        "expiresIn" : "5184000",
        "expiryMailNotifFlag" : -1,
        "extraPhotos" : 0,
        "fetchFromDate" : null,
        "fetchOrder" : "oldest",
        "fetchUntilDate" : "04/11/2014",
        "locale" : "en_US",
        "optIn" : true,
        "timezone" : "5.5",
        "userID" : "100000207309657"
    }
],
"maxLinkedAccounts" : 4,
"name" : "venu wale",
"notifications" : [
    {
        "_id" : ObjectId("51275fb7389b85f222000001"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:08:23.419Z")
    },
    {
        "_id" : ObjectId("51275fe8389b85f222000002"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:09:12.385Z")
    },
    {
        "_id" : ObjectId("51276020389b85f222000003"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:10:08.275Z")
    },
    {
        "_id" : ObjectId("5127605d389b85f222000004"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:11:09.946Z")
    },
    {
        "_id" : ObjectId("51276097389b85f222000005"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:12:07.384Z")
    },
    {
        "_id" : ObjectId("512760d2389b85f222000006"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:13:06.933Z")
    }
],
"password" : "638cfc167e8431c01227e4f113ec9427821b12493ed6fd3",
"phone" : "5555555555",
"photo_updated_time" : 1361341819,
"photosProcessed" : 15,
"photosToProcess" : 400,
"plan" : {
    "type" : "photoOnly"
},
"salt" : "af8ab2f5866642f03d31aa3f4a24e25e287bedc7",
"updated_on" : ISODate("2013-02-19T12:42:07.835Z")

我想从通知数组中删除任何一个对象。我正在使用此查询。

db.users.update({电话: '5555555555'},{$取消设置:{通知:{$ elemMatch:{_ ID:的ObjectId( '51276020389b85f222000003')}}}})

但它不起作用。

1 个答案:

答案 0 :(得分:4)

您只需notifications数组中的pull元素:

db.collection.update({phone:"5555555555"},{$pull:{notifications: {_id:ObjectId("51276020389b85f222000003")}}})

使用pull,您可以删除与内部查询匹配的所有元素。下一个$ pull

{$pull:{ notifications: {type: "limitUsed"}}}

将删除“limitUsed”类型的所有通知。