我有一个 monogoDB,其中包含以下结构的数据:
{ "Locales" : {
"Invariant" : {
"Checksum" : "7B4C15B697AAFC1484679076667267D2",
"Metadata" : {
"app" : {
"@appType" : "app",
"@id" : "284896113",
"categories" : {
"category" : [{
"@id" : "6011",
"@parentid" : "36",
"@type" : "GENRE",
"##text##" : "Music"
}, {
"@id" : "6016",
"@parentid" : "36",
"@type" : "GENRE",
"##text##" : "Entertainment"
}],
"##category" : null
},
"rating" : "2.5",
}
}
},
"ModifiedDate" : ISODate("2012-09-07T09:07:58.218Z")
} }, "_id" : "selection/live/app(284896113)"
我的数据库中大约有 100,000 个。我需要找出几件事。首先,其中有多少个具有“类别”数组,然后有多少个在其下方具有一个或多个“类别”数组,最后,其中有多少个具有 ##text## 值?
干杯
最佳答案
您可以使用$exists:
db.things.count( {
'Locales.Invariant.Metadata.app.categories': {$exists: true }} );
db.things.count( {
'Locales.Invariant.Metadata.app.categories.category': {$exists: true }} );
db.things.count( {
'Locales.Invariant.Metadata.app.categories.category.##text##':
{$exists: true }} );
请注意,这将返回文档的计数,而不是匹配的计数。在您的示例中,它将返回“1”,因为只有一个文档,而不是“2”(对于两次出现的 ##text##)。
关于mongodb - 在数组内 native 查询 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12530322/