获取具有MongoDB中的条件的所有数组子文档

时间:2014-05-23 09:53:13

标签: mongodb aggregation-framework

我有一个包含大量文档的集合:

{

    "Items" : [],
    "Technicians" : [],
    "_id" : ObjectId("537b5ea4c61b1d1743f4341f"),
    "budgets" : [ 
        {
            "concepts" : [ 
                {
                    "position" : 0,
                    "description" : "A",
                    "price" : "1",
                    "qty" : "11",
                    "total" : 11
                }, 
                {
                    "position" : 1,
                    "description" : "A",
                    "price" : "2",
                    "qty" : "22",
                    "total" : 44
                }, 
                {
                    "position" : 2,
                    "description" : "A",
                    "price" : "3",
                    "qty" : "33",
                    "total" : 99
                }, 
                {
                    "position" : 3,
                    "description" : "A",
                    "price" : "4",
                    "qty" : "44",
                    "total" : 176
                }
            ],
            "date" : "2014-05-21T10:20:48.696Z",
            "id" : 9989,
            "status" : "joooder",
            "total" : 500
        }, 
        {
            "id" : 260,
            "date" : "2014-05-22T11:12:40.260Z",
            "concepts" : [ 
                {
                    "position" : 0,
                    "description" : "Nueva",
                    "price" : "1",
                    "qty" : "1",
                    "total" : 1
                }
            ],
            "total" : 1,
            "status" : "pending"
        }, 
        {
            "id" : 111,
            "date" : "2014-05-22T13:36:28.111Z",
            "concepts" : [ 
                {
                    "position" : 0,
                    "description" : "Blabla",
                    "price" : "1",
                    "qty" : "11",
                    "total" : 11
                }
            ],
            "total" : 11,
            "status" : "pending"
        }
    ]
}

现在,我想得到预算,但只有预算,如果状态是待定的,但我需要在所有收集文件中找到。

我试过了:

db.orders.aggregate(
  { $unwind : "$budgets" },
  { $match : {
     "budgets.status": "pending"
  }});

但这不行......

编辑二:

有了这个:

db.orders.aggregate(
  { $project : {
        budgets : 1 
    }},
  { $match : {
     "budgets.status": "pending"
  }});

我明白了:

{
    "result" : [ 
        {
            "_id" : ObjectId("537b5ea4c61b1d1743f4341f"),
            "budgets" : [ 
                {
                    "concepts" : [ 
                        {
                            "position" : 0,
                            "description" : "A",
                            "price" : "1",
                            "qty" : "11",
                            "total" : 11
                        }, 
                        {
                            "position" : 1,
                            "description" : "A",
                            "price" : "2",
                            "qty" : "22",
                            "total" : 44
                        }, 
                        {
                            "position" : 2,
                            "description" : "A",
                            "price" : "3",
                            "qty" : "33",
                            "total" : 99
                        }, 
                        {
                            "position" : 3,
                            "description" : "A",
                            "price" : "4",
                            "qty" : "44",
                            "total" : 176
                        }
                    ],
                    "date" : "2014-05-21T10:20:48.696Z",
                    "id" : 9989,
                    "status" : "joooder",
                    "total" : 500
                }, 
                {
                    "id" : 260,
                    "date" : "2014-05-22T11:12:40.260Z",
                    "concepts" : [ 
                        {
                            "position" : 0,
                            "description" : "Nueva",
                            "price" : "1",
                            "qty" : "1",
                            "total" : 1
                        }
                    ],
                    "total" : 1,
                    "status" : "pending"
                }, 
                {
                    "id" : 111,
                    "date" : "2014-05-22T13:36:28.111Z",
                    "concepts" : [ 
                        {
                            "position" : 0,
                            "description" : "Blabla",
                            "price" : "1",
                            "qty" : "11",
                            "total" : 11
                        }
                    ],
                    "total" : 11,
                    "status" : "pending"
                }
            ]
        }
    ],
    "ok" : 1
}

第一个状态项目未定。

0 个答案:

没有答案