【问题标题】:update mongoDB collection using nodejs// throw new TypeError('Update document requires atomic operators');使用 nodejs 更新 mongoDB 集合 // throw new TypeError('更新文档需要原子操作符');
【发布时间】:2020-10-27 12:18:50
【问题描述】:

所以我能够使用 nodejs / mongoDB 创建一个集合 使用此代码

  // create a collection and inserting the object
     dbo.collection('employees').insertOne(myobj, function(err, res) {
  //if there is an error throw error message
    if (err) throw err;
  // print out if collection is created
    console.log(colors.green("Collection created ✓"),'\n','\n',companyMotto, '-', 
   subDomain,'\n','\n');
  //close the db
    db.close();
    });

我试图找出一个代码来更新整个集合,如果他们的集合已经存在使用这个

  // create a collection and inserting the object
  dbo.collection('employees').updateOne(myobj, function(err, res) {
  //if there is an error throw error message
    if (err) throw err;
  // print out if collection is updated
    console.log(colors.green("Collection updated ✓"),'\n','\n',companyMotto, '-', 
   subDomain,'\n','\n');
  //close the db
    db.close();
    });

但我收到一个错误“...throw new TypeError('更新文档需要原子运算符');” 任何帮助将不胜感激

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    如果你想创建一个集合,你应该直接调用createCollectioninsertOne 用于将文档插入集合中,updateOne 用于更新集合中的特定文档

    Update document requires atomic operators 表示未提供更新操作。所以 mongodb 不知道你希望如何更新文档。例如$set 更新值,$push 将值推送到数组中

    您可以参考 nodejs 文档以了解应该如何调用 updateOneupdateOne(filter, update, options, callback)

    见: https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#updateOne

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多