0

我有一个看起来像这样的数据库:

{
    "_id": ObjectId("50525e55467f67da3f8baf06"),
    "client": "client1"
}{
    "_id": ObjectId("505262f0deed32a758410b1c"),
    "client": "client2"
}{
    "_id": ObjectId("505269e2062dae91946e3c26"),
    "client": "client3"
}{
    "_id": ObjectId("5052f469341bdc4e8ac1b226"),
    "client": "client4",
    "products": [
        {
            "name": "product1"
        },
        {
            "name": "product2"
        }
    ]
}

我的问题是,如何在不删除 client4 记录的情况下从 client4 的产品列表中删除 product2?输出应如下所示:

{
    "_id": ObjectId("50525e55467f67da3f8baf06"),
    "client": "client1"
}{
    "_id": ObjectId("505262f0deed32a758410b1c"),
    "client": "client2"
}{
    "_id": ObjectId("505269e2062dae91946e3c26"),
    "client": "client3"
}{
    "_id": ObjectId("5052f469341bdc4e8ac1b226"),
    "client": "client4",
    "products": {
        "name": "product1"
    }
}
4

1 回答 1

0

你可以使用$pull

db.coll.update({client:'client4'},{$pull : { products : { name : 'product1' }});
于 2012-09-14T09:22:23.887 回答