我正在尝试使用 node.js 在 mongodb 中插入和更新文档。我可以使用以下代码成功插入文档。
var obj = { jsonit:
{ users: {},
groups: {},
meta:
{ id: 'daf09a36-b961-3db4-3144-aa19479c70c7'},
name: 'John Doe',
meta: undefined,
loki: undefined
}
Collection.insert( obj ).then( function insertCollectioncb(object) {
obj = object;
});
之后我想更新整个文档,因为我不知道哪些键将被更改或哪些新键将被插入。我正在使用以下代码来更新我的文档。
var obj = jsonit:
{ users: {},
groups: {},
meta:
{ id: '0da70a1f-f5cd-0fd2-10c8-7aa421296057'} ,
name: 'John Doe',
creationDate: '2016-10-13T11:05:08.453Z',
income: 1000,
meta: null,
loki: null
}
Collection.update( { _id: '57ff860b240e9d50a42ec53c' }, obj ).then( function updateBranchcb(object) {
obj = object;
});
我可以在 mongo shell 中成功完成,但不能通过节点 js 完成。