Reputation: 163
Upon on the mongoose query of find ("-_id services.location"), the output on stringifying is:
[
{
"services":[
{
"location":[
17.4374614,
78.7482878
]
},
{
"location":[
17.4020637,
78.18400519324596
]
},
{
"location":[
17.56208737,
78.284005186526
]
},
{
"location":[
17.5620637,
78.3240051999554
]
}
]
}
]
Is there any way where i can get to access each individual value of location like( 17.4374614, 78.7482878,17.4020637....) from the output I got.
My schema is as follows:-
var serviceSchema = new mongoose.Schema({
location:[{type: Number}]
})
Upvotes: 2
Views: 360
Reputation: 4983
Updated the answer for:
Is there any way to render only the array of loc values without _id into the templates. I only need to render the values of array of loc
Here's how you can do it: https://mongoplayground.net/p/VhrZKxg6OC1
db.collection.aggregate([
{
$unwind: "$services"
},
{
$unwind: "$services.location"
},
{
$project: {
"_id": "$_id",
"location": "$services.location"
}
},
{
$group: {
_id: "_id",
loc: {
$push: "$location"
}
}
},
{
$project: {
"_id": 0,
"locationArray": "$loc"
}
}
])
This will result:
[
{
"locationArray": [
17.4374614,
78.7482878,
17.4020637,
78.18400519324597,
17.56208737,
78.284005186526,
17.5620637,
78.3240051999554
]
}
]
Hope it solves your query.
Upvotes: 0
Reputation: 61225
What you're looking for is the $reduce
and $concatArrays
operators.
[
{
"$project": {
"result": {
"$reduce": {
"input": "$services.location",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
}
}
}
]
Upvotes: 1