在 find ("-_id services.location") 的 mongoose 查询中,字符串化的输出是:
[
{
"services":[
{
"location":[
17.4374614,
78.7482878
]
},
{
"location":[
17.4020637,
78.18400519324596
]
},
{
"location":[
17.56208737,
78.284005186526
]
},
{
"location":[
17.5620637,
78.3240051999554
]
}
]
}
]
有什么方法可以让我从我得到的输出中访问每个单独的位置值,例如(17.4374614、78.7482878、17.4020637....)。
我的架构如下:-
var serviceSchema = new mongoose.Schema({
location:[{type: Number}]
})
最佳答案
您正在寻找的是 $reduce
和 $concatArrays
运营商。
[
{
"$project": {
"result": {
"$reduce": {
"input": "$services.location",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
}
}
}
]
关于arrays - 从 mongoose find() 查询中提取一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51337769/