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
1
vote
1 answer

Update Array using $in and $addtoSet Mongodb

I am using $in and $addToSet to update an array in my collections. Say I have this kind of my documents. { _id: objectId(1), //this is just an example id names: ['harry', 'hermione'] }, { _id: objectId(2), names: ['ron'] }, { …
Joshua Fabillar
  • 506
  • 2
  • 9
  • 24
1
vote
2 answers

MongoDB insert a document for every documents in another collection

I have a collection (users) containing some documents like so : { _id: ObjectId("56d45406be05db4022be51f9"), morecontent : "" }, { _id: ObjectId("56d45406be05db3021be32e3"), morecontent : "" } I would like to create a new document…
1
vote
1 answer

Update multiple subdocument without exact sub document level condition

I have collection with the following document: { "_id" : ObjectId("56cbf9cd75de3ee9057b23c7"), "title" : "Abc", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" :…
KTAnj
  • 1,346
  • 14
  • 36
1
vote
1 answer

Efficient way to modify the time format of a field for all documents in MongoDB

I have a collection contains three hundred million documents. Each document has a "created_at" field that specifies the time in a string format like this 'Thu Feb 05 09:25:38 +0000 2015' I want to change all the "created_at" field to a MongoDB…
Jim GB
  • 83
  • 1
  • 10
1
vote
1 answer

MongoDB find and modify request update

I have the following structure in my MongoDB database for a product : product_1 = { 'name':'...', 'logo':'...', 'nutrition':'...', 'brand':{ 'name':'...', 'logo':'...' }, 'stores':{[ …
Orelus
  • 963
  • 1
  • 13
  • 23
1
vote
1 answer

Why is my MongoDb query inserting an embedded document on Update?

This is my MongoDB query: db.events.update({date:{$gte: ISODate("2014-09-01T00:00:00Z")}},{$set:{"artists.$.soundcloud_toggle":false}},{multi:true,upsert:false}) Apparently I cannot use "artists.$.soundcloud_toggle" to update all artist documents…
Robjocky
  • 47
  • 1
  • 7
1
vote
2 answers

Mongo update while excluding certain fields

This sounds like it would be similar to a Mongo $set. In this case, I would like to update the document (update and/or delete fields) but ignore the field creatorName, keeping it the same. Example document: {id: 1, firstName: 'Testy', middleName:…
Chandrew
  • 16,987
  • 4
  • 24
  • 40
1
vote
2 answers

Casbah MongoDB, how to both add and remove values to an array in a single operation, to multiple documents?

After searching, I was unable to figure out how to perform multiple updates to a single field. I have a document with a "tags" array field. Every document will have random tags before I begin the update. In a single operation, I want to add some…
user1170533
  • 79
  • 1
  • 5
1
vote
1 answer

Acknowledge a 'batch' of writes with MongoDB

Using Mongo 2.4.9 with C# driver 1.8.3 With the following example: WriteConcern concern = WriteConcern.Unacknowledged; for(int i=0;i<100;i++){ if(i==99)concern=WriteConcern.Acknowledged; …
user2849000
  • 89
  • 1
  • 6
1
vote
1 answer

Count of rows affected in a multiple row mongodb update

Using the mongo shell what needs to added to an update/delete query to also return the number of rows that were affected by the query.
kapad
  • 611
  • 2
  • 11
  • 27
1
vote
0 answers

Mongodb sharding dropping writes

I have a set of documents containing counters that are periodically incremented using an upsert $inc command. I have several nodes as part of a sharded cluster. With sharding switched off for the database, all updates seem to work fine and the…
TheDeveloper
  • 402
  • 4
  • 7
1
vote
2 answers

MongoDB Update $push Cannot apply the positional operator without a corresponding query field containing an array

model: { "_id" : "a62107e10f388c90a3eb2d7634357c8b", "_appid" : [ { "_id" : "1815aaa7f581c838", "events" : [ { "_id" : "_TB_launch", "boday" : [ …
uuzhangyu
  • 19
  • 2
  • 4
0
votes
0 answers

MongoBulkWriteError':multi update is not supported for replacement-style update

I am trying to look up the data from another collection and updating the values but it is failing with the below error. And my query is like below .And I tried use multi:true but it is failing with the below error . Would appreciate any help Error…
Rose
  • 89
  • 5
  • 18
0
votes
1 answer

Updating array of objects with arrayFilters

I am trying to update an array of objects by applying a filter but no updates happen on any of the objects and no errors appear. I would like all objects according to my filter to be updated. I am using MongoDB version…
Gravity123
  • 1,130
  • 3
  • 21
  • 39
0
votes
1 answer

MongoDB - Update deep nested array

I'm using Mongoose and ExpressJS I have a schema like this: { name: 'test', array1: [ { type: 'big', array2: [ { name: 'xyz', value: 1 } ] } ] } I have to do some bulk inserts to…
Patryk
  • 29
  • 5