如何更新mongodb中的记录?

问题描述:

收集如何更新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 } }); 
    }); 
}); 

我得到类型错误:对象摇摆住宅“收藏”是不是一个函数。

我在做什么错在这里?

+0

你能解释一下你正在试图用一些示例文档来展示你的收藏结构吗? – chridam

+0

@chridam添加了集合结构,我无法获取集合对象 –

+0

为什么我收到此错误对象摇摆的属性'集合'不是函数。 –

试试这个:

db.collection('message').find().forEach(function(err,doc) { 
    doc.records.forEach(function(link){ 
     link.text = link.text.replace('aaa', 'bbb'); 
     link.save(); 
    }); 
}); 

它将更新的财产,将调用保存();