在mongodb(本机js或.NET驱动程序)中拉出子数组子中的元素

时间:2012-09-24 03:46:27

标签: arrays mongodb

我在Mongo中有这些数据:

{
    "_id": ObjectId("505fd43fdbed3dd93f0ae088"),
    "categoryName": "Cat 1",
    "services": [
        {
            "serviceName": "Svc 1",
            "input": [
-------------------------------------------------------------------------
                { "quantity": 10, "note": "quantity = 10" },
-------------------------------------------------------------------------
                { "quantity": 20, "note": "quantity = 20" }
            ]
        },
        {
            "serviceName": "Svc 2",
            "input": [
                { "quantity": 30, "note": "quantity = 30" },
                { "quantity": 40, "note": "quantity = 40" }
            ]
        }
    ]
}

现在我想拉出输入数组的元素{ "quantity" : 10, "note" : "quantity = 10" }。我怎么能用Mongo做什么?

1 个答案:

答案 0 :(得分:1)

您可以使用update运算符和services来执行此操作,使用$pull位置运算符来标识您要定位的db.collection.update( { "_id" : ObjectId("505fd43fdbed3dd93f0ae088"), 'services.serviceName': 'Svc 1' }, { $pull: { 'services.$.input': { "quantity" : 10, "note" : "quantity = 10" } } } ); 元素:

{{1}}