我想不使用“ $ unwind”对arraylist元素进行排序。
在下面的示例中,如果要按item.category对数据进行排序,则“ item”是数组列表,它没有给出期望的结果。但是,如果我使用$ unwind展平结构,则会得到所需的结果。
在我的实时数据中,数组列表中有大量数据,因此,如果我使用$ unwind会遇到严重的性能挑战,这就是为什么我在不放松的情况下寻找排序的原因,有没有欢迎的想法!
db.ordersdup.insert([
{ "_id" : 2, "item" : [
{ "category" : "cookies", "type" : "chocolate chip" },
{ "category" : "cake", "type" : "lemon" },
{ "category" : "cake", "type" : "chiffon"},
{ "category" : "brownies", "type" : "blondie" }
],
"amount" : 50 }
]);
Sample Query
=====================
db.ordersdup.aggregate([
{
"$project":{
"_id":0,
"Categories":"$item.category"
}
},
// {
$unwind:"$Categories"
},
{
$sort:{
"Categories":1
}
}
])
Results without $unwind
==========================
{
"Categories":[
[
"cookies",
"cake",
"cake",
"brownies"
]
]
}
My desired output would look like $unwind
{
"Categories":[
[
"brownies",
"cake",
"cake",
"cookies"
]
]
}
Results with Flatten (i.e used $unwind)
========================================
/* 1 */{
"Categories":"brownies"
},
/* 2 */{
"Categories":"cake"
},
/* 3 */{
"Categories":"cake"
},
/* 4 */{
"Categories":"cookies"
}
Mongodb Database Version : 3.4.7 community edition, Environment Windows.
Thanks,
Nat