如何更新mongodb中的记录?
问题描述:
'message':[{
'records':[{
'text':'aaa in stack overflow'
}]
}]
db.collection('message').find().forEach(function(err,doc) {
doc.records.forEach(function(link){
var text = link.text.replace('aaa', 'bbb');
db.collection('message').updateOne({_id: doc._id}, { '$set': { 'text': text } });
});
});
我得到类型错误:对象摇摆住宅“收藏”是不是一个函数。
我在做什么错在这里?
答
试试这个:
db.collection('message').find().forEach(function(err,doc) {
doc.records.forEach(function(link){
link.text = link.text.replace('aaa', 'bbb');
link.save();
});
});
它将更新的财产,将调用保存();
你能解释一下你正在试图用一些示例文档来展示你的收藏结构吗? – chridam
@chridam添加了集合结构,我无法获取集合对象 –
为什么我收到此错误对象摇摆的属性'集合'不是函数。 –