在 mongoDB 中将坐标数据转换为传统坐标对
Convert coordinate data to legacy coordinate pairs in mongoDB
我有一个以下列格式存储位置数据的集合:
{ "Place": "xyz", "Location": { "long": 42.34, "lat": 73.59 } }
我需要使用聚合查询将上述位置数据转换为遗留坐标对。这就是我需要的输出:
{ "Place": "xyz", "Location": [42.34, 73.59] }
我怎样才能做到这一点?坐标对的顺序很严格,即经度在前,纬度在后。
使用mongo $setUnion查询如下
db.collectionName.aggregate({
"$group": {
"_id": "$Place",
"Long": {
"$push": "$Location.long"
},
"Lat": {
"$push": "$Location.lat"
}
}
}, {
"$project": {
"Places": "$_id",
"_id": 0,
"Location": {
"$setUnion": ["$Lat", "$Long"]
}
}
}).pretty()
$setUnion performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setUnion ignores the duplicate entries. $setUnion ignores the order of the elements.
从上面的链接中提到 setUnion
忽略了顺序。因此它显示了一些错误的结果。为了找到这个,我尝试了很多 mongo 查询但没有成功。然后使用 mongo forEach 方法显示预期输出
db.collectionName.find({
"Location": {
"$exists": true
}
}).forEach(function(data) {
locationData = [];
locationData.push(data.Location.long);
locationData.push(data.Location.lat);
var document = {
Place: data.Place,
Location: locationData
};
printjson(document);
})
现在可以使用 mongoDB 3.2 使用 $concatArray
功能。所以聚合看起来像:
db.locations.aggregate({
"$group": {
"_id": "$Place",
"Long": {
"$push": "$Location.long"
},
"Lat": {
"$push": "$Location.lat"
}
}
}, {
"$project": {
"Places": "$_id",
"_id": 0,
"Location": {
"$concatArrays": ["$Long", "$Lat"]
}
}
})
也使用 $concatArrays
保留顺序
我有一个以下列格式存储位置数据的集合:
{ "Place": "xyz", "Location": { "long": 42.34, "lat": 73.59 } }
我需要使用聚合查询将上述位置数据转换为遗留坐标对。这就是我需要的输出:
{ "Place": "xyz", "Location": [42.34, 73.59] }
我怎样才能做到这一点?坐标对的顺序很严格,即经度在前,纬度在后。
使用mongo $setUnion查询如下
db.collectionName.aggregate({
"$group": {
"_id": "$Place",
"Long": {
"$push": "$Location.long"
},
"Lat": {
"$push": "$Location.lat"
}
}
}, {
"$project": {
"Places": "$_id",
"_id": 0,
"Location": {
"$setUnion": ["$Lat", "$Long"]
}
}
}).pretty()
$setUnion performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setUnion ignores the duplicate entries. $setUnion ignores the order of the elements.
从上面的链接中提到 setUnion
忽略了顺序。因此它显示了一些错误的结果。为了找到这个,我尝试了很多 mongo 查询但没有成功。然后使用 mongo forEach 方法显示预期输出
db.collectionName.find({
"Location": {
"$exists": true
}
}).forEach(function(data) {
locationData = [];
locationData.push(data.Location.long);
locationData.push(data.Location.lat);
var document = {
Place: data.Place,
Location: locationData
};
printjson(document);
})
现在可以使用 mongoDB 3.2 使用 $concatArray
功能。所以聚合看起来像:
db.locations.aggregate({
"$group": {
"_id": "$Place",
"Long": {
"$push": "$Location.long"
},
"Lat": {
"$push": "$Location.lat"
}
}
}, {
"$project": {
"Places": "$_id",
"_id": 0,
"Location": {
"$concatArrays": ["$Long", "$Lat"]
}
}
})
也使用 $concatArrays
保留顺序