我试图用Node.js的使用猫鼬到新的领域MongoDB中通过更新方法添加到现有的文档
当磁场存在猫鼬更新MongoDB的文件,更新工作,但如果外地做不存在,更新将不起作用。
我想添加新的字段到文档中。
MongoDB的查询(https://docs.mongodb.com/v3.2/reference/operator/update/set/#behavior)工作:
db.getCollection('users').update({ 'uid': 'uid' }, { $set: { 'vehicle_status': 'bike' } })
但在猫鼬完成时将无法正常工作。使用$ set不会添加字段。
User.update({ uid: uid }, { $set: { vehicle_status: vehicleSatus } }, { multi: true })
我发现灵魂。 http://stackoverflow.com/questions/23959287/add-a-field-to-existing-mongodb-document-with-mongoose-in-node-js?rq=1 model not defined new filed。我在模型中添加新的字段。并添加提交的工作。 – sabper
好的,有帮助的知道。请在这里发表您的问题时尽量拼出最好的答案,因为它们可能对很多人有用,并且有助于他们理解他们:) 让模型中的字段当然是必要的 –