0
從每個記錄陣列獨特的價值觀我有MongoDB
的集合,它看起來像這樣:獲取MongoDB中
{
"_id" : ObjectId("56d3e53b965b57e4d1eb3e71"),
"name" : "John",
"posts" : [
{
"topic" : "Harry Potter",
"obj_ids" : [
"1234"
],
"dates_posted" : [
"2014-12-24"
]
},
{
"topic" : "Daniel Radcliffe",
"obj_ids" : [
"1235",
"1236",
"1237"
],
"dates_posted" : [
"2014-12-22",
"2015-01-13",
"2014-12-24"
]
}
],
},
{
"_id" : ObjectId("56d3e53b965b57e4d1eb3e72"),
"name" : "Jane",
"posts" : [
{
"topic" : "Eragon",
"tweet_ids" : [
"1672",
"1673",
"1674"
],
"dates_posted" : [
"2014-12-27",
"2014-11-16"
]
}
],
}
我怎麼能查詢到得到這樣一個結果:
{
"name": "John",
"dates": ["2014-12-24", "2014-12-22", "2015-01-13"]
},
{
"name": "Jane",
"dates" : ["2014-12-27", "2014-11-16"]
}
我需要的日期是唯一的,因爲「2014-12-24」出現在"posts"
的兩個元素中,但我只需要一個。
我想這樣做db.collection.aggregate([{$unwind: "$posts"}, {$group:{_id:"$posts.dates_posted"}}])
這給了我的結果是這樣的:
{ "_id" : [ "2014-12-24", "2014-12-22", "2015-01-13", "2014-12-24" ] }
{ "_id" : [ "2014-12-27", "2014-11-16" ] }
我如何刪除重複項,還可以獲得對應日期的名字嗎?