meteor $ pull从数组中删除

时间:2013-10-14 14:27:00

标签: mongodb meteor

我的数据库中有以下对象:

{
    "_id": "fTgR2YtHiZBzzqF6J",
    "following": [
        {
            "user": {
                "_id": "S4dLHRJiuHoyAp26q",
                "fb": {
                    "id": "100006681067911",
                    "name": "Helen Amffhajfgiaa Laubergskymanwitzescusonsteinsen"
                }
            },
            "date": "2013-10-01T17:25:50.305Z"
        },
        {
            "user": {
                "_id": "MAyxz4Yk5F9vh9RRy",
                "fb": {
                    "id": "100006719587007",
                    "name": "Mary Amfgaiehgkg Smithman"
                }
            },
            "date": "2013-10-11T10:47:58.898Z"
        }
    ]
}

现在我要删除

{
   "user": {
      "_id": "MAyxz4Yk5F9vh9RRy",
      "fb": {
          "id": "100006719587007",
          "name": "Mary Amfgaiehgkg Smithman"
      }
    },
    "date": "2013-10-11T10:47:58.898Z"
}

数组中的子文档名为“follow”。

我的查询是这样的:

 Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
             "user": {
                 "_id": "MAyxz4Yk5F9vh9RRy"
             }
         }
     }
 });

但没有任何反应!

有人能指出我的错误吗?

3 个答案:

答案 0 :(得分:4)

我的问题是:我不得不拉动整个物体:

 Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
            "user": {
                "_id": "MAyxz4Yk5F9vh9RRy",
                "fb": {
                    "id": "100006719587007",
                    "name": "Mary Amfgaiehgkg Smithman"
                }
            },
            "date": "2013-10-11T10:47:58.898Z"
        }
         }
     }
 });

并且 NOT 只需要调用:

Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
             "user": {
                 "_id": "MAyxz4Yk5F9vh9RRy"
             }
         }
     }
 });

答案 1 :(得分:0)

问题与https://stackoverflow.com/a/17779687/149818完全相同。如果您只有1 "user",那么您通常可以查询并且仅适用于案例,但对于多个用户,您必须重新设计您的收藏

答案 2 :(得分:0)

您也可以尝试这个答案(Remove Item from Array in Meteor.js),我认为它更合适,代码更少。