MongoDB 更新查询以将现有字段设置到数组中
MongoDB update query to set existing field into array
我有以下 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 ]
}
}
你可以这样做:
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);
这将影响所有与查询确实匹配的记录。
此更改不能在一次操作中完成。您需要先获取需要更改的文档,然后使用新格式更新文档。
如果您使用 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
}
});
});
});
我有以下 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 ] } }
你可以这样做:
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);
这将影响所有与查询确实匹配的记录。
此更改不能在一次操作中完成。您需要先获取需要更改的文档,然后使用新格式更新文档。
如果您使用 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
}
});
});
});