查询以过滤mongodb集合中的信息

时间:2015-04-13 07:07:06

标签: mongodb aggregation-framework

我必须从符合条件的文档列表中检索用户列表。文档结构如下所示

{
"_id" : ObjectId("660ff865d4f9075d40a1101c"),
"orderFormId" : "OF-rJw4elBYK",
"orderDetails" : [ 
    {
        "courseId" : "53fc31f443fa1fe885d3ad61",
        "userInfo" : [ 
            {
                "dob" : "2015-03-22T18:30:00.000Z",
                "lastName" : "M",
                "status" : "Pending Appproval",
                "eMail" : "jihin345@baabte.com",
                "firstName" : "Arun"
            }, 
            {
                "status" : "requested",
                "firstName" : "asdasd",
                "dob" : "2015-03-23T18:30:00.000Z",
                "lastName" : "asdafasd",
                "userId" : "RQ-11xDPALgR",
                "eMail" : "adsasd@baabte.com"
            }, 
            {
                "status" : "requested",
                "firstName" : "asdaf",
                "dob" : "2015-03-23T18:30:00.000Z",
                "lastName" : "fsdsdf",
                "userId" : "RQ-OdoXAOLrB",
                "eMail" : "ashdjasufh@baabte.com"
            }, 
            {
                "status" : "requested",
                "firstName" : "asdas",
                "dob" : "2015-03-23T18:30:00.000Z",
                "lastName" : "asdasd",
                "userId" : "RQ-Bw2Xokmda",
                "eMail" : "asdasd@gmail.com"
            }
        ],
        "userCount" : 5,
        "Name" : "Compilers",
        "coursePrice" : 1000,
        "coursetype" : "offline"
    }, 
    {
        "courseId" : "53fc31f443fa1fe885d3ad62",
        "userInfo" : [ 
            {
                "dob" : "2015-03-22T18:30:00.000Z",
                "lastName" : "Raj",
                "status" : "requested",
                "eMail" : "jihin432@baabte.com",
                "firstName" : "Nithul"
            }, 
            {
                "dob" : "2015-03-22T18:30:00.000Z",
                "lastName" : "P C",
                "status" : "requested",
                "eMail" : "jihin345@baabte.com",
                "firstName" : "Kahyoom"
            }
        ],
        "userCount" : 1,
        "Name" : "Computer Science 101",
        "coursePrice" : 0,
        "coursetype" : "offline"
    }, 
    {
        "courseId" : "57fc31f443fa1fe885d3ad64",
        "userInfo" : [ 
            {
                "status" : "requested",
                "firstName" : "asdasd",
                "dob" : "2015-03-23T18:30:00.000Z",
                "lastName" : "aasdasd",
                "userId" : "RQ-WqEXBkjv5",
                "eMail" : "asdasd@gmail.com"
            }
        ],
        "userCount" : 1,
        "coursePrice" : 0,
        "Name" : "Introduction to Haptics: Self-Paced",
        "coursetype" : "offline"
    }
],
"companyId" : ObjectId("54128cc57525614f6e3e710a"),
"createdDate" : ISODate("2015-03-23T11:26:29.027Z"),
"updatedDate" : ISODate("2015-03-24T15:00:33.248Z"),
"crmId" : ObjectId("660ab20bd4f9075d40a10d52"),
"urmId" : ObjectId("660ab20bd4f9075d40a10d52"),
"activeFlag" : 0,
"customCompanyCode" : "baa-106",
"status" : "Pending approval"}

从上面的文档中我必须得到在此文档中存在的userInfo对象中具有“已批准”状态的用户。我创建了一个查询,但这将输出具有不同status.My查询的所有用户,如下所示,

db.clnTrainingRequest.find({companyId:ObjectId('54128cc57525614f6e3e710a'), "orderDetails.userInfo.status":{$in:['Approved']}}).toArray()

请任何人帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

您可以使用aggregation。您需要$unwind orderDetailsuserInfo数组,然后使用$match为用户提供status Approved

db.clnTrainingRequest.aggregate(
    [   { "$match": { "companyId": ObjectId('54128cc57525614f6e3e710a') },
        { "$unwind": "$orderDetails" }, 
        { "$unwind": "$orderDetails.userInfo" }, 
        { "$match": { "orderDetails.userInfo.status": "Approved" }}
    ]
)