1

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

{
    "_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"
}

来自称为“以下”的数组的子文档。

我执行此操作的查询如下所示:

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

但是什么也没有发生!

有人可以指出我的错误吗?

4

3 回答 3

4

我的问题是:我必须 $pull 整个对象:

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

并且不能只是这样称呼:

Collection.update({"_id":"fTgR2YtHiZBzzqF6J"},
 {
    "$pull": {
        "following": {
             "user": {
                 "_id": "MAyxz4Yk5F9vh9RRy"
             }
         }
     }
 });
于 2013-10-14T15:55:20.477 回答
0

问题与https://stackoverflow.com/a/17779687/149818完全相同。您查询一般是正确的,并且如果您恰好有 1 ,则仅适用于案例"user",但对于多个用户,您必须重新设计您的收藏

于 2013-10-14T14:55:26.593 回答
0

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

于 2014-02-12T19:29:14.753 回答