我有以下mongo子文档
"location" : { "zipCode" : "90670", "lat" : "33.942669", "lng" : "-118.074384", "city" : "Santa Fe Springs", "state" : "California", "streetAddress" : "12131 Telegraph Road, 2nd Floor", }
有人可以建议我使用mongo更新查询或节点功能来获取以下文档
"location" : { "zipCode" : "90670", "city" : "Santa Fe Springs", "state" : "California", "streetAddress" : "12131 Telegraph Road, 2nd Floor", "geo" : { "type" : "Point", "coordinates" : [ 33.942669 ,-118.074384 ] } }
答案 0 :(得分:0)
你可以这样做:
var query = {
"zipCode" : "90670",
"city" : "Santa Fe Springs",
"state" : "California",
"streetAddress" : "12131 Telegraph Road, 2nd Floor"
};
var operator = {
$unset: {
"lat" : "",
"lng" : ""
},
$set: {
"geo" : {
"type" : "Point",
"coordinates" : [ 33.942669 ,-118.074384 ]
}
}
};
var options = {
multi: true
}
db.yourCollection.update(query, operator, options);
这将影响您确实与查询匹配的所有记录。
答案 1 :(得分:0)
此更改无法在单个操作中完成。您需要先获取需要更改的文档,然后使用新格式更新文档。
答案 2 :(得分:0)
如果您使用node.js mongodb驱动程序,您可以执行以下操作:
var firstQuery = {
"zipCode" : "90670",
"city" : "Santa Fe Springs",
"state" : "California",
"streetAddress" : "12131 Telegraph Road, 2nd Floor"
};
db.collection('yourCollection').find(firstQuery).toArray(function(err, results) {
if (err) return err;
results.forEach(function(record) {
var geo = {
"type" : "Point",
"coordinates" : [ record.lat ,record.lng ]
};
var newRecord = {
zipCode: record.zipCode,
city: record.city,
state: record.state,
streetAddress: record.streetAddres,
geo: geo
};
db.collection('yourCollection').update(record, newRecord, function(err) {
if(err) {
//do whatever you want with this
}
});
});
});