如何从中更新收藏集中的所有文档:
{
"_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]
}
MongoDB聚合操作处理数据记录并返回计算结果。
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
或$merge
操作员获取聚合管道返回的文档,并将其写入指定的集合(您不能在源集合中插入)。技巧部分:将结果写入新集合中并丢弃旧集合。
db.collection.aggregate([
{
$project: {
"_id": 1,
"place_id": "$pickUpPlace.place_id",
"location": [
"$pickUpPlace.latitude",
"$pickUpPlace.longitude"
]
}
},
{ $out : "collection2" }
])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句