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

MongoDB - Update with $set and $first operators

I'm trying to update a field with the first element of an array. The model: { name: { type: ObjectId }, names: { type: [ObjectId] } } The request: Model.updateMany({}, { $set: { name: { …
1
vote
1 answer

MongoDB - Update field in nested array

This is my collection structure in MongoDB: { _id: id, name: name, loans: [ [{id: 1, acName: "ABC"}], [{id: 2, acName: "DEF"}], [{id: 3, acName: "GHI"}] ] } How can I update acName of a specific ID? Let's say, I want to update…
1
vote
1 answer

MongoDB - $subtract is not working in updateMany()

This one is not working with 0 result found: user.updateMany( { status: { $in: ["registered", "applied"] }, createdAt: { $lt: { $subtract: [new Date(), 1000 * 3600 * 24 * 7] } } }, { $set: { status: "rejected" }…
Kanish
  • 273
  • 4
  • 10
1
vote
2 answers

Difference between the UpdateOne() and findOneAndUpdate Methods in Mongo DB

What is the difference between the UpdateOne() and the findOneAndUpdate() methods in Mongo DB? I can't seem o understand their differences. Would appreciate it if a demonstrative example using UpdateOne() and findOneAndUpdate could be used.
Bosser445
  • 303
  • 1
  • 9
1
vote
1 answer

PyMongo - Update the field for the object in nested array

Trying to update the nested object for the document in MongoDB. { "items" : [ { "id": 1, "name": "a", "child": [ { "id": 11, "name": "aa" }, { "id": 12, "name": "bb" }, ] }, …
1
vote
1 answer

MongoDB - Update data type for the nested documents

I have this collection: (see the full collection here https://mongoplayground.net/p/_gH4Xq1Sk4g) { "_id": "P-00", "nombre": "Woody", "costo": [ { "tipo": "Cap", "detalle": "RAC", "monto_un": "7900 ", "unidades":…
Godoy32
  • 35
  • 5
1
vote
1 answer

Update nested array object value by replacing substring of that object property

I have the following set of data in my MongoDB collection called orders: { "_id" : ObjectId("618e0e1b17687316dcdd6246"), "groupUID": "abc", "orderData" : { "charges" : { "total" : 18480.0, "subtotal" :…
Syed Muhammad Oan
  • 687
  • 2
  • 15
  • 39
1
vote
0 answers

MongoDB updating elements in array of objects from google functions

I have a collection for tracking some tasks performed by google functions { _id: someid, tasks: [ { taskId: "1", status: "PENDING" }, { taskId: "2", status: "PENDING" …
1
vote
1 answer

MongoDB - How to modify the "key" element in the document

I am having the below document structure: [ { "network_type": "ex", "rack": [ { "xxxx": { "asn": 111111, "nodes": { "business": [ "sk550abcc1eb01.abc.com", …
Oudadham
  • 23
  • 4
1
vote
1 answer

How can I update a field starting with dollar in an embedded document in MongoDB?

I have a document in MongoDB in a collection with a document which has a field x which value is an embedded document, created this way: > db.c.insert({_id: 1, x: {$a: 2, b: 3}}) WriteResult({ "nInserted" : 1 }) > db.c.findOne({_id: 1}) { "_id" : 1,…
fgalan
  • 11,732
  • 9
  • 46
  • 89
1
vote
0 answers

MongoDB Update Query Nested Array

I have a data structure for a typical movie theatre: { "_id": ObjectId("5bdadf3k28ds2sdfdasbf321"), "seats": [ [ { "status": "VACANT", "code": "1A" }, { "status":…
user1955934
  • 3,185
  • 5
  • 42
  • 68
1
vote
0 answers

(MongoDB) Aggregate (.out) moove values to wrong fields

I'm actually creating an autocomplete website bar using express js & mongodb 4.2.7, and using lat & lon to avoid using geocoding api. Here is the format of my db: { "_id" : ObjectId("5eea03e9a7891b6d701df571"), "id_ban_position" :…
user13493056
1
vote
1 answer

Mongoose/MongoDB when Update subdocument array at the same time, it updates but cant find the document that updated later

i have a document which has subdocument array in it(size is 6), then i remove 5 element at the same time, it perfectly updates array, but cant find the document after updated, here the code to update it await gameRoom.findOneAndUpdate({…
Jsennin
  • 153
  • 2
  • 12
1
vote
2 answers

MongoDB - Updating TTL Index value

I'm trying to update the value of the expiry date in a document. Here's the Node.js code I use to set the index everytime I want to update the document: database.collection(collectionName).createIndex({ "expires_on": 1 }, { expireAfterSeconds: 0…
1
vote
1 answer

Exception in thread "main" com.mongodb.MongoWriteException: The array filter for identifier 'element' was not used in the update

Exception in thread "main" com.mongodb.MongoWriteException: The array filter for identifier 'element' was not used in the update { $set: { leave_history: { leave_history.$[element].pl_used: 6, leave_history.$[element].cl_used: 6,…
Ali
  • 83
  • 1
  • 7