0
Q:

mongodb updateone nodejs


  var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";

  
MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  
  var dbo = db.db("mydb");
  
  var myquery = { address: "Valley 345" };
  var newvalues = { 
  $set: {name: 
  "Mickey", address: "Canyon 123" } };
  dbo.collection("customers").updateOne(myquery, 
  newvalues, function(err, res) {
    if (err) throw err;
    
  console.log("1 document updated");
    
  db.close();
  });
}); 
2
const query = { "name": "football" };
const update = {
  "$push": {
    "reviews": {
      "username": "tombradyfan",
      "comment": "I love football!!!"
    }
  }
};
const options = { "upsert": false };

itemsCollection.updateOne(query, update, options)
  .then(result => {
    const { matchedCount, modifiedCount } = result;
    if(matchedCount && modifiedCount) {
      console.log(`Successfully added a new review.`)
    }
  })
  .catch(err => console.error(`Failed to add review: ${err}`))
3

New to Communities?

Join the community