Last active
January 14, 2019 10:27
-
-
Save pablocattaneo/2e27d524a84e9181f73e1ff9beaf07c3 to your computer and use it in GitHub Desktop.
How to update an collection in mongodb? #mongoDb #mongoShell Source: https://www.udemy.com/mongodb-the-complete-developers-guide/learn/v4/t/lecture/11739206?start=0
List of update operators: https://docs.mongodb.com/manual/reference/operator/update/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.collectionName.updateOne( | |
{ "name" : "Pizza Rat's Pizzaria" }, | |
{ $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } }, | |
); | |
#the first parameter is the filter, it mean that mongo is going to update the collection that match with this creteria |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If you need to use objectId first you need to import it | |
const ObjectId = require('mongodb').ObjectId | |
const db = require('../db') | |
db.getDb() // Connect db | |
.db() | |
.collection('collectionName') | |
.updateOne({"_id" : ObjectId("5c39cc1db9f2a512ef04fa86")}, {$set: {"name": "Robertt"}}) | |
.then((r)=> { | |
console.log("Result", r.result) | |
}).catch(err => { | |
console.log(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment