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

MongoDB - How to move an array from one array field to another

I have two fields in a MongoDB document publish and draft with schema as: { publish: [[ { type: String, required: true }, { type: String, required: true } ]], draft: [[ { type:…
Fallen
  • 91
  • 6
0
votes
2 answers

MongoDB update an array element matching _id and multiple conditions within the array

I have a MongoDB collections as: Collection: [ { "_id": ObjectId("64390a7196f544082a671469"), "name": "Sydney", "issues": [ {"vol": "2", "num": "1", "freq": 5}, {"vol": "1", "num": "2", "freq": 7} ] }, { "_id":…
sariDon
  • 7,782
  • 2
  • 16
  • 27
0
votes
2 answers

Update all documents in a collection in Mongodb with a new field

I have a collection named employees with documents like. { first_name:"john", last_name:"doe" } I want to run an update query which adds a new field like {name:'john doe'} I know it can be done like…
0
votes
1 answer

How to update value in array in MongoDB

For example, Documents. [ { "username": "joy", "size_info": [ { "data1": "apple bear cat", "data2": 100 }, { "data1": "dog eat fog good ", "data2": 100 }, { "data1":…
jexists
  • 171
  • 1
  • 10
0
votes
1 answer

MongoDB - Increment a field using another array field

I have a collection with the document of this structure: { "_id": { "$oid": "63e8af476a3674484ea14888" }, "my_id": 321123, "version": 0, "parameters": [ {"a": 1}, {"a": 1, "b": 2} ] } Using a RESTful API the user can change…
Netanel
  • 459
  • 1
  • 5
  • 17
0
votes
0 answers

Mongo db update a nested array with many elements in a nested array

I have a db that looks like this : { "_id": "637c648fb8fcfb2bc3071bb9", "consultant_name": "Sam smith", "consultantUsername": "sam", "consultant_Password": "123", "type": "consultant", "clients": [ { …
Vaodi
  • 37
  • 6
0
votes
1 answer

MongoDB - How to convert the data structure of a column and store it in another column

There are 3 million rows in MongoDB. I want to create column B using column A. A : [1,2,3,4] // All level value is 1 B : [ { key: 1, level: 1 }, { key: 2, level: 1 }, { key: 3, level: 1 }, { key: 3, level: 1 }, ]; What I have…
Kundan
  • 590
  • 9
  • 21
0
votes
1 answer

Performing an update on the path '_id' would modify the immutable field '_id', send a object as a json format in the body

This is my controller: const updateUser = async (req, res) => { const uid = req.params.uid const updates = req.body console.log(updates) const status = await dao.updateUser(uid, updates) res.json(status) …
qwe123
  • 51
  • 1
  • 3
0
votes
1 answer

MongoDB PHP driver - How to use $replaceAll

How to substitute part of the string in MongoDB with an update query. For example, I have this: _id:…
0
votes
0 answers

Mongo DB Failed to target Upsert by query :: could not extract exact shard key', details={}}

I am trying to insert the document which is received by input system in mongodb database by using upsert option but it is failing with below error : Anyone help me how to perform the upsert to avoid the duplicate transactions based on field **Write…
Rose
  • 89
  • 5
  • 18
0
votes
0 answers

java.lang.IllegalArgumentException: Invalid BSON field name orders

I want to insert the documents without any duplicates .I tried with createIndex but MongoDB can enforce a uniqueness constraint on a ranged shard key index. Through the use of a unique index on the shard key.For an already-sharded collection, you…
Rose
  • 89
  • 5
  • 18
0
votes
1 answer

Command failed with error 67 :cannot create unique index over { Key.IdentifierValue:: -1 } with shard key pattern { _id: "hashed" }'

I need to avoid duplications Transactions while inserting the documents in MongoDB database . Getting below exception .The condition is if we try to insert the Key.IdentifierValue with the same value it should throw the duplicate exception but the…
Rose
  • 89
  • 5
  • 18
0
votes
1 answer

MongoDB .NET Driver - Using pullFilter to remove string from string array

I am trying to do a pullFilter and can get it to work on complex types. await _Collection.UpdateOneAsync( Builders.Filter.Eq(d => d.Id, id), Builders.Update .Set(d => d.UpdatedBy, actioner) .Set(d => d.UpdatedOn,…
0
votes
1 answer

Spring Data MongoDB - how to update using @Update without setting most fields to null?

After reading Spring's docs at https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.repositories.queries.update , I wrote this Repository method: @Repository interface TokenRepo: MongoRepository { …
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0
votes
1 answer

MongoDB - Way of update a specific element in array

To update a specific element in the array on MongoDB collection. MongoDB data - { _id: new ObjectId("63608e3c3b74ed27b5bdf703"), placename: "khulna", frnds: [ { name: "osama", cost: "2121" }, { name: "lotid", cost: "2121" }, {…