【问题标题】:Update Whole Document in MongoDb在 MongoDb 中更新整个文档
【发布时间】:2016-10-13 13:25:26
【问题描述】:

我正在尝试使用 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中成功,但不能通过node js。

【问题讨论】:

  • 您是否收到错误消息或什么?
  • 第二个代码的obj定义格式不正确。
  • @DaveV 不,我没有收到任何错误
  • @MiguelGonzalez 抱歉,我已经修复了有问题的错字。实际上我不是自己创建这些变量,我只是在这里提到它们,以便人们可以更好地了解我的对象的实际外观

标签: javascript node.js mongodb lokijs


【解决方案1】:

试试这个

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 }, true
Collection.update( { _id: '57ff860b240e9d50a42ec53c' }, obj ).then( function updateBranchcb(object) {
        obj = object;
      });

问题是一个特别紧的花括号

 meta: 
    { id: '0da70a1f-f5cd-0fd2-10c8-7aa421296057'} },

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2016-10-15
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多