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
2
votes
1 answer

Randomizing unique data with mongo?

I'm trying to scramble all the email properties in my db. email is defined in the Mongoose model as unique. Here's the script I'm attempting to run in the shell db.getCollection('users').update( {}, {$set{ …
user773737
2
votes
2 answers

Meteor.users.update access denied when adding a new field to current document in collection

I'm looking to add a new field (array that will be populated with $push in the future) to the current document but am getting the error update failed: MongoError: Cannot apply $push/$pushAll modifier to non-array I'm working on the Meteor.users…
2
votes
2 answers

How to update an element within an array of a sub-document in Mongodb

Facing issue while trying to update an element within an array of sub document in Mongo. If I consider the following document in the collection "resource" { "_id": 1, "resources": [ { "resource_id": 1, …
user3315068
  • 91
  • 2
  • 7
2
votes
1 answer

Update value mongo using Java

I am using Java Mongo driver for the DB interaction. I have regular updates to be performed on the DB rows and the object that is quite nested. Something like this : MyObject: { _id: dbGeneratedId, myId: "A String ID that i created", …
Some guy
  • 1,210
  • 1
  • 17
  • 39
2
votes
2 answers

Mongodb update several documents with different values

i am trying to update several documents in mongodb with different values each. In mysql I do something like this: $objs = array(array('id'=>1,'lat'=>37.123,'lng'=>53.123),...,array('id'=>n,'lat'=>x,'lng'=>y)); $sql = "INSERT INTO objects…
leojg
  • 1,156
  • 3
  • 16
  • 40
2
votes
2 answers

mongoDB : updating an objects using dot notation (multi-level object)

I have an object with the following form : { "_id": ObjectId("4fa43f4d1cf26a6a8952adf1"), "userId": "1", "facebookId": "1234", "groups": [{ "groupName": "testGroup", "members": [{ "memberFirstName":…
IRousso
  • 136
  • 2
  • 7
1
vote
2 answers

Accessing $map variable in $cond

I have some data in a MongoDB collection that looks something like this: db.test.insertOne( { "interactions": [ { data: "keep", prompt: "prompt 1" }, { other:…
Bernie
  • 2,253
  • 1
  • 16
  • 18
1
vote
1 answer

MongoDB .NET Driver - How to increment double nested field

The goal: to increment double nested field with the specific identifier in the entity. The example of the document here: competition_dota2 { "id" : "b350c9fd-3632-4b0a-b5cb-66e41d530f55", "type" : "COMPETITION_TYPE_ESPORTDOTA2", …
bynik
  • 67
  • 4
1
vote
2 answers

MongoDB: Cannot increment with non-numeric argument

I have a collection of User documents, containing several fields and an array of Bets. I am trying to set up an update that I will run on a schedule. In the User documents, the Balance field needs to be incremented by (Bets[index].multiplier *…
batson
  • 11
  • 3
1
vote
1 answer

how to Increment optional sub-fields using $inc operator?

this are sample documents of the collection, { _id: ObjectId("637f1128d8298d42bae0d4fc"), name: 'Iska Paphat', age: 8, cat: { name: 'Malone Poppelhoffen', age: 7 } }, { _id: ObjectId("637f1128d8298d42bae0d4fd"), name:…
1
vote
2 answers

MongoDB - How to add a new item into an array

How do I use the method () to implement the following changes? Add a new experience "HIVE" to the employee whose empeId is 'e001'. and Change the email account for employee e001 to "jamesbond$hotmail.com". Below is the database in…
Bryead
  • 120
  • 6
1
vote
1 answer

MongoDB - arrayFilters and updateMany from same document

I have the following sample of data: { _id: 1, seniorityDate: '2001-01-01T00:00:00Z', assigned: [ { groupId: 11, system: 'Dep', effectiveDate: null }, { groupId: 12, system: 'Team', …
Mehran Ishanian
  • 369
  • 2
  • 4
  • 13
1
vote
2 answers

MongoDB - How to set the value of a field if it doesn't exist and remain the field if it is already presented in a document

So assume I have a document as shown below: { "_id" : ObjectId("6336d94e0330f5d48e44fb0f"), "systemId" : "5124301", "userId" : "9876543210", "tempId" : "123456da87sdafasdf", "updatedAt" : ISODate("2022-09-30T12:01:11.714+0000"), …
naved17
  • 156
  • 1
  • 11
1
vote
1 answer

MongoDB - Modify the field name in a nested list of lists

As the title says, the field I need to modify is nested like this: basicData.owners.relatedJson.basicData.devices.equipmentID which owners and devices are both lists. The object looks like this: { "basicData": { "owners": [ { …
mal
  • 3,022
  • 5
  • 32
  • 62
1
vote
1 answer

MongoDB .NET Driver - How to push an element into an array which is inside an array

I am working on inserting an element inside an array which is inside an object which is inside an array. { "_id": "63206e8cc876d082460e33c3", "MessageTrackingId": "08eab450-a2bd-408a-afab-be9ea665503e", "RequestMessageLog": { …
1 2
3
12 13