如何从此更新收藏夹中的所有文档:
{
"_id" : "16-00-003",
"pickUpPlace" : {
"place_id" : "someId",
"latitude" : 21.912363052368164,
"longitude" : 24.804792404174805
}
}
对此:
{
"_id" : "16-00-003",
"place_id" : "someId",
"location" : [21.912363052368164, 24.804792404174805]
}
答案 0 :(得分:0)
MongoDB aggregation操作处理数据记录并返回计算结果。
db.collection.aggregate([
{
$project: {
"_id": 1,
"place_id": "$pickUpPlace.place_id",
"location": [
"$pickUpPlace.latitude",
"$pickUpPlace.longitude"
]
}
}
])
db.collection.aggregate([...]).forEach(function(doc){
db.collection.save(doc);
})
$out
or $merge
运算符获取聚合管道返回的文档,并将其写入指定的集合(您不能在源集合内部插入)。 技巧部分:在新集合中写入结果并删除旧集合。
db.collection.aggregate([
{
$project: {
"_id": 1,
"place_id": "$pickUpPlace.place_id",
"location": [
"$pickUpPlace.latitude",
"$pickUpPlace.longitude"
]
}
},
{ $out : "collection2" }
])