简体   繁体   中英

update mongoDB collection using nodejs// throw new TypeError('Update document requires atomic operators');

so im able to create a collection using nodejs / mongoDB using this code

  // 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();
    });

im trying to figure out a code to update the collection as a whole if they collection already exists using this

  // 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();
    });

but im getting an error "...throw new TypeError('Update document requires atomic operators');" any help would be greatly appreciated

If you want to create a collection, you should just call createCollection instead. insertOne is for you to insert a document into a collection and updateOne is for you to update a particular document in a collection

Update document requires atomic operators means an update operation is not supplied. So mongodb doesn't know how you want the document to be updated. eg $set to update a value, $push to push a value into an array

You can refer to the nodejs doc to see how updateOne is supposed to be called. updateOne(filter, update, options, callback)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM