I have document structure like :
{
map: 'A',
points: [
{
type: 'type1',
distanceToSpawn: 110
},
{
type: 'type4',
distanceToSpawn: 40
},
{
type: 'type6',
distanceToSpawn: 30
}
]
},
{
map: 'B',
points: [
{
type: 'type1',
distanceToSpawn: 100
},
{
type: 'type2',
distanceToSpawn: 60
},
{
type: 'type3',
distanceToSpawn: 25
}
]
},
{
map: 'C',
points: [
{
type: 'type2',
distanceToSpawn: 90
},
{
type: 'type3',
distanceToSpawn: 1
},
{
type: 'type6',
distanceToSpawn: 76
}
]
}
I want to get all maps having point type type1
sorted by the distanceToSpawn
in ascending order.
Expected result :
{
map: 'B',
points: [
{
type: 'type1',
distanceToSpawn: 100
}
]
},
{
map: 'A',
points: [
{
type: 'type1',
distanceToSpawn: 110
}
]
}
I've tried something like :
db.maps.find({'points.type': {$eq : 'type1'}}, {map: 1, 'points.$':1}).sort({'points.distanceToSpawn': 1}).limit(10)
But this thing not sorting maps by ascending order.
Thanks.
To sort the whole array by value, or to sort by array elements that are not documents, identify the input array and specify 1 for an ascending sort or -1 for descending sort in the sortBy parameter.
Filter MongoDB Array Element Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.
You cannot do that with arrays, and the main problem here is because you want the "sort" to happen on the matched element. If you want to sort results like this then you need to use .aggregate()
instead. Either as:
For modern MongoDB versions:
db.maps.aggregate([
{ "$match": { "points.type": "type1" }},
{ "$addFields": {
"order": {
"$filter": {
"input": "$points",
"as": "p",
"cond": { "$eq": [ "$$p.type", "type1" ] }
}
}
}},
{ "$sort": { "order": 1 } }
])
For MongoDB 2.6 to 3.0
db.maps.aggregate([
{ $match: { 'points.type': 'type1' } },
{
$project: {
points: {
$setDifference: [
{
$map: {
input: '$points',
as: 'p',
in: {
$cond: [
{ $eq: ['$$p.type', 'type1'] },
'$$p',
false,
]
}
}
},
[false]
]
}
}
},
{ $sort: { 'points.distanceToSpawn': 1 } },
]);
Or less efficiently in versions prior to MongoDB 2.6:
db.maps.aggregate([
{ "$match": { "points.type": "type1" }},
{ "$unwind": "$points" },
{ "$match": { "points.type": "type1" }},
{ "$group": {
"_id": "$_id",
"points": { "$push": "$points" }
}},
{ "$sort": { "points.ditanceToSpawn": 1 } }
])
That's the only way to match the correct elements and have them considered in a "sort" operation. The default cursor sort will just otherwise consider the values for the field in the array elements that do not match your selected "type" instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With