查询具有不同集合中的计数的组密钥

时间:2017-08-14 21:35:01

标签: mongodb mongodb-query

我正在尝试对在不同集合中找到的一组键值进行分组。我需要将结果作为每个集合的Value和Count。所以DivisionName Chicago可以同时出现在第一个集合和第二个集合中。

另一个问题是我最多可以拥有15个Submitted.SubmittedForms集合。问题是输出没有结合值。例如,我将为芝加哥分两行。每个集合一个,两行后总计数。

"SubmittedForms" : {
    "SubmittedForms" : {
        "0" : {
            "FullName" : "John Smith",
            "EmailAddress" : "'testmail@att.net",
            "PhoneNumber" : "(555) 555-5555",
            "MarketId" : LUUID("24c111da-bc1c-b14c-8ee9-a4e9d25d4a91"),
            "Comments" : "Please send me more information                                    ",
            "EmailSent" : "1",
            "InternalEmailSent" : "0",
            "SubmittedTime" : ISODate("2017-04-27T15:10:17.507Z"),
            "DivisionName" : "Columbus",
            "CommunityName" : "Test Ridge",
            "ProductType" : "Test"
        },
        "1" : {
            "FullName" : "John Smith",
            "EmailAddress" : "'testmail@att.net",
            "PhoneNumber" : "(555) 555-5555",
            "MarketId" : LUUID("67c503da-bc1c-b14c-8ee9-a4e9d25d4a91"),
            "Comments" : "Please send me more information                                    ",
            "EmailSent" : "1",
            "InternalEmailSent" : "0",
            "SubmittedTime" : ISODate("2017-04-27T15:10:17.507Z"),
            "DivisionName" : "Austin",
            "CommunityName" : "Test Another Ridge",
            "ProductType" : "Test Product"
        },
        "2" : {
            "FullName" : "John Smith",
            "EmailAddress" : "'testmail@att.net",
            "PhoneNumber" : "(555) 555-5555",
            "MarketId" : LUUID("adfadsf-bc1c-b14c-8ee9-a4e9d25d4a91"),
            "Comments" : "Please send me more information                                    ",
            "EmailSent" : "1",
            "InternalEmailSent" : "0",
            "SubmittedTime" : ISODate("2017-04-27T15:10:17.507Z"),
            "DivisionName" : "Austin",
            "CommunityName" : "The Village",
            "ProductType" : "Test"
        },
        "3" : {
            "FullName" : "John Smith",
            "EmailAddress" : "'testmail@att.net",
            "PhoneNumber" : "(555) 555-5555",
            "MarketId" : LUUID("y4444-bc1c-b14c-8ee9-a4e9d25d4a91"),
            "Comments" : "Please send me more information                                    ",
            "EmailSent" : "1",
            "InternalEmailSent" : "0",
            "SubmittedTime" : ISODate("2017-04-27T15:10:17.507Z"),
            "DivisionName" : "Columbus",
            "CommunityName" : "Test Case",
            "ProductType" : "Test"
        }
    }
},

这是我需要看到的: 芝加哥23 亚特兰大12 纽约14

这就是我试图做的事情:

db.Contacts.group({
"key": {
"SubmittedForms.SubmittedForms.0.DivisionName": {"SubmittedForms.SubmittedForms.0.DivisionName":1,$exists:true,$ne:null,$where: "this.name.length > 0"},
"SubmittedForms.SubmittedForms.1.DivisionName": {"SubmittedForms.SubmittedForms.1.DivisionName":1,$exists:true,$ne:null,$where: "this.name.length > 0"}
},
"initial": {
"countDivision": 0
},
"reduce": function(obj, prev) {
  if (true != null) if (true instanceof Array) prev.countstar += true.length;
    else prev.countDivision++;
},
"cond": {
    _id: {
        "$ne": ["/TESTING.COM/i"]
    },
    "SubmittedForms.SubmittedForms.0.SubmittedTime": {
        "$gte": new ISODate("2017-08-12 07:47:00Z")
    }
}
});

This is the current structure and were the submitted forms live.

0 个答案:

没有答案