在子文档中搜索

时间:2016-09-06 12:29:53

标签: node.js mongodb subdocument

您好我想在联系人对象中获取syncFlag的所有联系人。

I tried this solution but only returns only one sub document

我也试过这个解决方案,它会返回所有匹配的文件

How to find document and single subdocument matching given criterias in MongoDB collection

以下是示例文档

{
    "_id" : ObjectId("57ce7d6c7387d533bfa2d45c"),
    "ITBCompanyId" : 2608,
    "updatedAt" : ISODate("2016-09-06T12:19:35.972Z"),
    "createdAt" : ISODate("2016-09-06T08:25:16.325Z"),
    "name" : "This is test",
    "identifier" : "THDNOM2",
    "addressLine1" : "Valencia Lahore",
    "syncFlag" : true,
    "orgId" : "1",
    "deletedAt" : null,
    "whois" : [ 
        {
            "test" : "noman"
        }
    ],
    "configuration" : [ 
        {
            "test" : "noman"
        }
    ],
    "contact" : [ 
        {
            "firstName" : "Active",
            "_id" : ObjectId("57ceb04811f005420b7ed54a"),
            "syncFlag" : false,
            "communicationItems" : [],
            "customFields" : []
        }, 
        {
            "firstName" : "Active",
            "_id" : ObjectId("57ceb04811f005420b7ed54b"),
            "syncFlag" : false,
            "communicationItems" : [],
            "customFields" : []
        }, 
        {
            "firstName" : "Active",
            "_id" : ObjectId("57ceb44b5f8b534bc312aacd"),
            "syncFlag" : true,
            "communicationItems" : [],
            "customFields" : []
        }, 
        {
            "firstName" : "Active",
            "_id" : ObjectId("57ceb457f141fd4c1c98a748"),
            "syncFlag" : true,
            "communicationItems" : [],
            "customFields" : []
        }
    ],
    "agreement" : [ 
        {
            "test" : "noman"
        }
    ],
    "companySite" : [ 
        {
            "test" : "noman"
        }
    ],
    "companyNote" : [ 
        {
            "test" : "noman"
        }
    ],
    "type" : {
        "name" : "Client"
    },
    "status" : {
        "name" : "Active"
    },
    "id" : "19493",
    "__v" : 0,
    "_info" : {
        "updatedBy" : "Omer",
        "lastUpdated" : ISODate("2016-09-06T11:52:07.000Z")
    }
}

预期产出:

 ITBCompanyId: 1,
    contact: [{
               "firstName" : "Active",
                "_id" : ObjectId("57ceb04811f005420b7ed54b"),
                "syncFlag" : true,
                "communicationItems" : [],
                "customFields" : []

    }]

1 个答案:

答案 0 :(得分:0)

试试这个。您应该根据需要获取文档。下面我使用了一个聚合查询来根据需要格式化mongodb响应。有关mongodb聚合的更多信息,请参阅this。为了形成这个查询,我提到了this堆栈溢出帖子。

db.companies.aggregate(
    { $match: {"contact.syncFlag": true }},
    { $unwind: '$contact'},
    { $match: {"contact.syncFlag": true }},
    { $group: { 
            _id: '$_id',  
            contact: {$push: '$contact'},
    }
});

如果您希望文档中包含所有其他字段。你可以试试这个。

db.companies.aggregate(
    { $match: {"contact.syncFlag": true }},
    { $unwind: '$contact'},
    { $match: {"contact.syncFlag": true }},
    { $group: { 
        _id: '$_id', 
        ITBCompanyId: {$first: '$ITBCompanyId'}, 
        updatedAt: {$first: '$updatedAt'}, 
        createdAt: {$first: '$createdAt'}, 
        name: {$first: '$name'}, 
        identifier: {$first: '$identifier'}, 
        addressLine1: {$first: '$addressLine1'}, 
        syncFlag: {$first: '$syncFlag'}, 
        orgId: {$first: '$orgId'}, 
        deletedAt: {$first: '$deletedAt'}, 
        whois: {$first: '$whois'}, 
        configuration: {$first: '$configuration'}, 
        contact: {$push: '$contact'},
        agreement: {$first: '$agreement'},
        companySite: {$first: '$companySite'},
        companyNote: {$first: '$companyNote'},
        type: {$first: '$type'},
        status: {$first: '$status'},
        id: {$first: '$id'},
        _info: {$first: '$_info'},
    }
});
相关问题