数据库如下所示:
"INC" : {
"SIIB" : {
"Description" : "Interest Income, Bank",
"Value" : 66.06941
},
"STIE" : {
"Description" : "Total Interest Expense",
"Value" : 6.90925
},
"ENII" : {
"Description" : "Net Interest Income",
"Value" : 59.16016
如何获得所有不同的描述?
答案 0 :(得分:1)
您可能正在寻找这样的东西:
db.getCollection('pets').aggregate({
$unwind: "$Annual" // flatten the "Annual" array into separate documents
}, {
$project: { // transform "INC" subdocuments into key-value pair
"inc": {
$objectToArray: "$Annual.INC"
}
}
}, {
$group: { // get all distinct values across all documents
"_id": null,
"distinctValues": { $addToSet: "$inc.v.Description" }
}
})
答案 1 :(得分:0)
您可以尝试仅获取数组中的distinct
/ Unique
描述。
db.getCollection('Authors').distinct("Annual.INC.SIIB.Description")
在Mongo GUI中测试。
样本文档:
{
"_id" : ObjectId("5b6a9bf571ace9eea8e64a04"),
"name" : "userName",
"avatar" : "avtarDetail",
"Annual" : [
{
"INC" : {
"SIIB" : {
"Description" : "Interest Income, Bank",
"Value" : 66.06941
},
"STIE" : {
"Description" : "Total Interest Expense",
"Value" : 6.90925
},
"STIEE" : {
"Description" : "Total Interest Expense",
"Value" : 6.90925
}
}
}
]
}
输出:
[
"Interest Income, Bank"
]