Thursday, September 29, 2022

Update, delete object in MongoDB

Update individual field in collection database object

db.collection.updateOne({title: "3 title"}, {$set:{counter: 3}})


Insert new string in collection database object

db.collection.update({}, {$set: {counter: 1}}, {$inc : {counter: 1}}, {upsert : true})


Delete string from collection database object

db.example.updateMany({},{"$unset":{"tags.words":1}})


Delete collection object by id

db.test_users.deleteOne( {"_id": ObjectId("4d512b45cc9374271b02ec4f")})


Delete all documents from collection

db.collection.deleteMany({})

https://www.mongodb.com/docs/mongodb-shell/crud/delete/


db.collection.update({ { _id : id }, { $pull: { "list": "four" } }});


db.collection.updateMany( { URL: { $regex: /Long("/ } }, [{ $set: { URL: { $replaceOne: { input: "$URL", find: "Long("", replacement: "" }  }}  }])


db.getCollection('collectionName').updateMany({$inc : {counter: 1}}, {upsert : true})


db.getCollection('collectionName').updateMany({counter: {$in : ["some title",  "some title 3"]}}, {$inc : {counter: 1}}, {upsert : true})


db.collection.aggregate([ { $group:{"_id":null, "data":{ $push:"$$ROOT" } } }, { $unwind:{ "path" : "$data", includeArrayIndex: 'counter' }  },  {  $addFields:{ "data.counter":{  $sum:["$counter", 1] }} },  {   $replaceRoot:{ "newRoot":"$data" } }])


For this, set “unique:true” i.e. the unique constraint and avoid inserting duplicates as in the below syntax

db.cards.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true})


https://www.mongodb.com/basics/create-database

https://stackoverflow.com/questions/57982189/is-there-a-way-to-add-a-counter-to-mongodb-query

https://www.tutorialspoint.com/mongodb/mongodb_autoincrement_sequence.htm

https://www.tutorialspoint.com/mongodb/mongodb_update_document.htm

https://stackoverflow.com/questions/61964449/mongodb-update-multiple-documents-solution-update-document-counter-or-insert

https://stackoverflow.com/questions/12589792/how-to-replace-substring-in-mongodb-document

https://stackoverflow.com/questions/57982189/is-there-a-way-to-add-a-counter-to-mongodb-query

https://stackoverflow.com/questions/29487351/how-to-convert-string-to-numerical-values-in-mongodb

https://stackoverflow.com/questions/29487351/how-to-convert-string-to-numerical-values-in-mongodb

https://stackoverflow.com/questions/8218484/mongodb-inserts-float-when-trying-to-insert-integer

https://database.guide/7-ways-to-count-documents-in-mongodb/

https://stackoverflow.com/questions/24985684/mongodb-show-all-contents-from-all-collections

https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field

https://stackoverflow.com/questions/1740023/mongodb-how-to-update-multiple-documents-with-a-single-command

https://www.w3resource.com/mongodb/mongodb-field-update-operator-$inc.php

https://stackoverflow.com/questions/71732533/add-number-field-in-project-mongodb?rq=1

https://stackoverflow.com/questions/66206713/trying-to-iterate-through-a-collection-of-mongodb-results-to-update-another-coll

https://www.baeldung.com/mongodb-upsert

No comments:

Post a Comment