【问题标题】:Search in subdocument在子文档中搜索
【发布时间】:2017-01-13 21:58:21
【问题描述】:

您好,我想在联系人对象中获取所有同步标志为 true 的联系人。

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" : []

    }]

【问题讨论】:

  • 添加预期结果的文档。
  • @tarashypka 我有问题告诉过我只想找到syncFlag true的联系人对象
  • 那么您的预期输出应该包含两个对象:{ "firstName" : "Active", "_id" : ObjectId("57ceb44b5f8b534bc312aacd"), "syncFlag" : true, "communicationItems" : [], "customFields" : [] }, { "firstName" : "Active", "_id" : ObjectId("57ceb457f141fd4c1c98a748"), "syncFlag" : true, "communicationItems" : [], "customFields" : [] }
  • 您希望"ITBCompanyId""updatedAt" 和其他字段出现在集合中每个文档的输出中吗?或者您只想要集合中所有文档的唯一联系人数组?
  • 我只想要同步标记为 true 的联系人数组,是的,我想要 ITBCompanyId

标签: node.js mongodb subdocument


【解决方案1】:

试试这个。您应该按要求获取文件。下面我使用了一个聚合查询来根据需要格式化 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'},
    }
});

【讨论】:

  • 一种解决方案适用于有限的字段,另一种适用于包含所有字段的情况。
  • 不要复制粘贴完全相同的查询。将集合名称更改为您的集合的适当名称,在我的回答中,我已将集合名称指定为公司。
  • 先生,您好,我知道我必须根据我的收藏名称更改收藏名称。但这不起作用,这就是我将其粘贴在这里的原因。我问你为什么这两种解决方案都不起作用的问题,看看你在回答什么,这是非常明显的
  • { "result" : [ { "_id" : ObjectId("57ce7d6c7387d533bfa2d45c"), "contact" : [ { "firstName" : "Active", "_id" : ObjectId("57ceb44b5f8b534bc312aacd") ,“syncFlag”:真,“communicationItems”:[],“customFields”:[]},{“firstName”:“Active”,“_id”:ObjectId(“57ceb457f141fd4c1c98a748”),“syncFlag”:真,“communicationItems " : [ ], "customFields" : [ ] } ] } ], "ok" : 1 }
  • 我使用第一个查询得到的响应
猜你喜欢
  • 2015-06-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 2014-06-25
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
相关资源
最近更新 更多