Questions tagged [mongodb-update]

Modifies an existing document or documents in a collection.

The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter.

By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria.

The update() method has the following form:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Source

195 questions
112
votes
11 answers

How to replace substring in mongodb document

I have a lot of mongodb documents in a collection of the form: { .... "URL":"www.abc.com/helloWorldt/..." ..... } I want to replace helloWorldt with helloWorld to get: { .... "URL":"www.abc.com/helloWorld/..." ..... } How can I achieve this for…
user1071979
  • 1,701
  • 2
  • 12
  • 25
27
votes
8 answers

MongoDB concatenate strings from two fields into a third field

How do I concatenate values from two string fields and put it into a third one? I've tried this: db.collection.update( { "_id": { $exists: true } }, { $set: { column_2: { $add: ['$column_4', '$column_3'] } } }, false, true ) which doesn't…
26
votes
9 answers

How can I change a MongoDB field to a single element array?

I have a field in mongodb that's a string: {"field": "some text"}. I want to convert them all into single-element arrays containing the string: {"field": ["some text"]}. I know I can just loop through all the documents, get the field, then update,…
Harry
  • 52,711
  • 71
  • 177
  • 261
18
votes
4 answers

How to add key-value pair to object in MongoDB

If I have a document with the following basic structure: { ... Monday: { a:1, b:2 }, Tuesday: { c:3, d:4 } ... } Am I able to 'push' an additional key:value pair to Monday's value? Result would be: { Monday: { a:1, b:2, z:8 }, Tuesday:…
flimflam57
  • 1,334
  • 4
  • 16
  • 31
11
votes
1 answer

MongoDB $pull query not remove and update the existing record

My main concern is about in mongodb documentation they use $elemMatch command for find and pull the element from array, but this is not work when i use. My document structure is { "_id" : ObjectId("55dea445c3ad8cac03a7ed8e"), "email" :…
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
8
votes
2 answers

Default value not set while using Update with Upsert as true

I have the following model for users: var UserSchema = new mongoose.Schema({ name: String, dob: Date, sex: String, photo: String, email: {type: String, index: {unique: true, required: true}}, created: {type: Date, default:…
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
6
votes
4 answers

Update/remove subdocument in MongoDB collection

Note:Please use mongodb shell to execute the codes. Let's say i have one student document as below { "_id" : 4, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 }, { "grade" : 85, "mean" :…
Mayur Kataria
  • 675
  • 1
  • 10
  • 14
6
votes
1 answer

Can't append to array using string field name [$] when performing update on array fields

rowsI am attempting to perform a mongodb update on each field in an array of records. An example schema is below: { "_id" : ObjectId("508710f16dc636ec07000022"), "summary" : "", "uid" : "ABCDEF", "username" : "bigcheese", "name"…
HGPB
  • 4,346
  • 8
  • 50
  • 86
4
votes
2 answers

Condtional Update in MongoDb

Is there a better way of doing this conditional update with only one db call i.e only using user.update? user.findOne({ fbPsid: sender }, 'referal', function (err, res) { if (res.referal.length < 5 ) { user.update( {…
pulankit
  • 555
  • 4
  • 9
4
votes
1 answer

Update an embedded document with $set

I have a document in this form { "firstName": "John", "lastName": "Doe", "preferences": { "xxx": true, "yyy": false, "zzz": true } } I want to update specific fields in the preferences embedded document. If I use the…
tano
  • 836
  • 1
  • 10
  • 25
4
votes
1 answer

MongoDB $pull array 2 level

I'm trying to pull an element in an array with e two level deep complexity My document : > db.users.find({ mail : 'titi@toto.fr'}).pretty() { "_class" : "bean.User", "_id" : ObjectId("52f504bb2f9dd91186211537"), "commandes" :…
4
votes
4 answers

MongoDB: Update property of subarray just updates the first element

The matching element looks like that: { "_id": { "$oid": "519ebd1cef1fce06f90e3157" }, "from": "Tester2", "to": "Tester", "messages": [ { "username": "Tester2", "message": "heeey", …
David
  • 560
  • 2
  • 10
  • 26
3
votes
3 answers

Mongoose - How to push object in nested array of objects

I have this data structure in my MongoDB database: "menu": [ { "dishCategory":"61e6089f209b802518e2b4a4", "dishMeals": [ { "dishMealName": "Burger King", …
mne_web_dev
  • 261
  • 4
  • 12
3
votes
3 answers

MongoDB using $cond with Update ($inc)

Is there any way to use $cond along with ($set, $inc, ...) operators in update? (MongoDB 4.2) I want to update a field in my document by $inc it with "myDataInt" if a condition comes true, otherwise keeps it as it is: db.mycoll.update( {"_id" :…
Mishooq
  • 31
  • 1
  • 4
3
votes
1 answer

Deep update of Mongo DB document

I have the following document structure in Mongo DB 3.0 document: { id: "ID", name: "NAME", items:[ { id:"100", name:"Item Name", fields:[ {a:"field 1", b:44}, …
vladtax
  • 203
  • 1
  • 2
  • 9
1
2 3
12 13