我在mongodb中有一个layer
文档,其中包含地图上的点。这些点是一个数组。要更新特定索引处的点,我使用以下代码:
var query;
if (req.body.isNew)
query = { $push: { 'points': point } }
else
query = { $set: { ['points.' + pointIndex]: point } };
console.log("QUERY IS: ", query)
Layers.update({ _id: layer._id }, query, (err, result) => {
if (err || !layer) {
console.error(err);
res.status(400).json({ "error": "error updating" })
return;
}
res.status(200).json({ success: true });
});
console.log("RAN query");
您将看到的是,正确生成查询以更新给定索引的特定代码。但是,当mongoose运行查询时,它会用undefined
替换点!它为什么这样做?这是控制台输出(我设置了mongoose来记录它运行的查询):
QUERY IS: { '$set':
{ 'points.0':
{ _id: '58389c52d1fc2dcb1d9f06d1',
lat: 39.605688178320804,
lng: -98.9208984375,
customFieldValues: {},
tasks: [],
name: 'abcd',
updatedAt: 2016-11-25T20:17:32.995Z,
updatedBy: 'bobbington@mailinator.com' } } }
RAN query
Mongoose: layers.update({ _id: ObjectId("58387d06b549e03c588993eb") }, { '$set': { 'points.0': undefined } }, {})
答案 0 :(得分:0)
Downgraded to Mongoose 4.5.3 and it works! This is why I should be using yarn instead of NPM.