1
我想将我的mongodb集合从2d修改为2dsphere。 我有这个结构在我db.users:从同一个Mongodb集合中坐标数组创建GeoJson
{
"_id" : "1c6930387123e960eecd65e8ade28488",
"interests" : [
{
"_id" : ObjectId("56a8b2a72f2c2a4c0250e896"),
"coordinates" : [-3.6833, 40.4]
}
],
}
我想有是这样的:
{
"_id" : "1c6930387123e960eecd65e8ade28488",
"interests" : [
{
"_id" : ObjectId("56a8b2a72f2c2a4c0250e896"),
"loc":{
"type":"Point",
"coordinates" : [-3.6833, 40.4]
}
}
],
}
我尝试这样:
db.users.update({interests:{$elemMatch:{coordinates : { $exists: true } }}}, { $set: { 'interests.$.place':{"type":"Point", "coordinates":'interests.$.coordinates'} } }, { upsert: false, multi: false });
很明显,它直接插入“'interest。$。坐标
并试图在这个节点:
users.find({interests:{$elemMatch:
{coordinates :
{ $exists: true } }}} ,
{"interests.coordinates":1,"interests._id":1,"_id":0 }).forEach(function(doc){
users.update({interests:
{$elemMatch:
{_id : doc.interests[0]['_id'] }}},
{ $set: { 'interests.$.loc':{"type":"Point", "coordinates":doc.interests[0]['coordinates']} } },
{ upsert: false, multi: false },function(err, numberAffected, rawResponse) {
var modified=numberAffected.result.nModified;
console.log(modified)
});
});
但坐标插入混合值。
想法?
谢谢!
你真了不起! 你的代码工作正常! 只有我不得不将这个“doc.interests.coordinates.length”修改为这个“doc.interests.length”并且它非常完美! 非常感谢! –
谢谢!我修正了那个输入错误! – Findemor