不能回填大于1500000个元素的数组
Can't backfill array to larger than 1500000 elements
我有一个mongodb文档命名为collection:
{
_id: ObjectId('53e9dd54c784717558c46997'),
bloks: [ /* subdocument array */ ],
sections: [ /* subdocument array */ ],
chapters: [ /* subdocument array */ ]
}
和每个子文档都有id
和state
字段作为其他字段之间的字符串。
当使用mongoose时,我可以更新bloks
并使用
update = { bloks: [ /*some changed array*/ ] };
Collection.findByIdAndUpdate(collection._id, update, function (err, collection) {
if (err) {
// return an error
} else {
// return the collection
}
});
但是当我尝试在其他数组中更新特定的section和chapter状态时:
update = {
bloks: [ /*some changed array*/ ],
'sections.140439739188823467.state': 'some state',
'chapters.1404397391757313579.state': 'some state'
};
我得到一个错误:
Can't backfill array to larger than 1500000 elements
我如何更新集合文档与块,节和章节的数据,并有它的当前值?
请注意,我使用.findByIdAndUpdate()
,因为它更有效,我有问题,使.update()
方法做实际保存。
感谢Leonid Beschastny在问题中的评论,我发现我正在使用子文档id作为数组中的索引,因此我修改了代码以找出正确的。
sindex = _.findIndex(collection.sections, function (section) {
return sid === section.id;
});
if (-1 < sindex) {
update['sections.' + sindex + '.state'] = 'state';
}
hindex = _.findIndex(collection.chapters, function (chapter) {
return hid === chapter.id;
});
if (-1 < hindex) {
update['chapters.' + hindex + '.state'] = 'state';
}
实际上,有一种方法可以通过其id更新子文档的字段,使用位置操作符$
:
db.collection.update({
_id: collection._id,
'sections.id': '140439739188823467'
}, {
'sections.$.state': 'some state'
});
这种方法的唯一问题是不能使用单个查询更新多个子文档。因此,它需要两个请求来更新sections
和chapters
。
您还应该考虑使用mongoose
子文档特性。在现有的模式中,您唯一需要更改的是子文档的主id字段,因为mongoose
总是使用_id
字段作为主标识符。
它将允许您使用MongooseDocumentArray::id
helper,特别为您的情况设计:
sindex = collection.sections.id(sid);
hindex = collection.sections.id(hid);
还有一件事。
因为你已经抓取你的文档,没有必要你发出一个findAndModify
操作,因为当你调用.save()
方法在mongoose
文档它发出update
操作,发送到MongoDB只更新字段:
collcetion[sindex].state = 'new state';
collcetion[hindex].state = 'new state';
collcetion.save(next);
// Mongoose sends update operation, setting only this two fields
此外,mongoose使用版本控制来确保子文档数组中文档的顺序没有改变,从而保护您不会更新错误的子文档。
相关文章:
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- 用于检查数组中是否存在元素的javascript自定义方法
- 在数组中的一个元素上设置多个值
- 从组件状态的数组中删除元素
- Mongoose-在更新中删除数组元素
- 从数组中删除元素的最佳方法是:javascript/jquery
- 数组中元素(字符串)的总和
- javascript数组元素是否知道其封闭数组
- 查找数组javascript中包含的元素类型
- 如何在javascript上向数组的对象添加新元素
- 如果数组仍在执行,async.forEach()将运行我添加到数组中的新元素
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- 一些元素没有从数组中删除
- JS.循环遍历多维数组,以计数元素在每列中的出现次数
- 将数组元素附加到FormData dos'不适用于Firefox 15
- 元素不会添加到Javascript数组中
- 如何从另一个带下划线的数组中筛选带元素的数组
- 如何将函数应用于两个数组的第 n 个元素(数组的数组)
- Js接受两个值之间的元素(数组)