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

Updating with unique object values in MongoDB

Collection in MongoDB looks like the following: { _id:"Some random ID", name:"ABC", profiles:[ { profileId: '123', attachedOn: 2022-07-28T15:08:11.072Z }, { profileId: '456', attachedOn: 2022-07-28T15:08:11.072Z …
0
votes
2 answers

MongoDB - How to add a field to all object within an array

I have a document with a field called info, and info has a field inside it called data. data is an array of objects. I want to add a new boolean field, isActive: false, to each object in data, with updateMany. This is how it looks now { info:…
TheStranger
  • 1,387
  • 1
  • 13
  • 35
0
votes
1 answer

Mongoose - Update multi objects inside an array by ID

Firstly, sorry for my bad English. Secondly, I want to ask, how to multi-update my document. I have a row structure like this: Data Model [ { "id": '00001', "exp": '192', "items": [ { "qty": 23, "ID":…
0
votes
0 answers

Is it possible/ how to perform upsert on a sharded MongoDB cluster

I’d like to perform an upsert operation on a sharded MongoDB cluster, but I get the exact same error for every variation I try. When I write this way to a local standalone Mongo, it works perfectly, but on the sharded cluster I get the errors. My…
0
votes
1 answer

MongoDB - How to rename the specific field from list of unstructured array field?

I have several documents as given below. Now I need to do rename the middlename field into mid_name if middlename exists in the document. { "id":"abc", "name":[ { "first_name":"abc", "last_name":"def" }, …
0
votes
1 answer

MongoDB adding ObjectId to array of IDs

I have Poems schema which has a linked array of ObjectIds under field name 'communities': { _id: ObjectId("61659ef70e87b90018f7baa1"), schemaName: 'Poem', helps: [ ObjectId("5d15c609832d390c41ab6872") ], communities: […
PYP
  • 125
  • 13
0
votes
1 answer

MongoDB - Update nested document based on its current content

I have a simple collection with a document like this one: { _id: ObjectId('62b196e43581370007773393'), name: 'John', jobs: [ { company: 'ACME', type: 'programmer', technologies: [ …
cichystefan
  • 328
  • 2
  • 10
0
votes
2 answers

Replace field value with first element of an array in the same document

I have two models // Product model const ProductSchema = { vendors: [{ type: Schema.Types.ObjectId, ref: 'Vendor' }], mainVendor: { type: Schema.Types.ObjectId, ref: 'Vendor' }, } // Vendor model const VendorSchema = { ... } When I delete a…
0
votes
2 answers

MongoDB - How to find and update elements in a nested array

Here is the collection: db.employees.insertMany([ { "data": { "category": [ { "name": "HELLO", "subcategory": [ "EDUCATION", "ART", ] }, { …
Fahd Lihidheb
  • 671
  • 4
  • 20
0
votes
1 answer

How to compare two fields of one subdocument in array?

Given some documents in MongoDB [ { "id": 1, "items": [ { "id": "T0001", "x": 10, "y": 10 }, { "id": "T0002", "x": 10, "y": 5 }, { "id": "T0003", …
Indiana
  • 313
  • 4
  • 8
0
votes
1 answer

MongoDB update change/convert an array with string elements to an array with object elements using the existing/current value within that object

I am learning MongoDB (5.6) and have a collection with documents like this: [ { "_id": { "$oid": "62642b53df03395a48eba4d3" }, "title": "Chemical Biology", "authors": [ "625861411edf9d62ec72c889" ], "year":…
sariDon
  • 7,782
  • 2
  • 16
  • 27
0
votes
1 answer

MongoDB - How to update a specific property of a nested array element

I have a collection with the following structure: { arrangements: [ { displayName: "MRT.8" }, { displayName: "MRT.10" }, { displayName: "MRT.12" }, (...) ] } I want the substring MRT to be replaced with MOBILE, so the result…
tiefenauer
  • 669
  • 7
  • 19
0
votes
1 answer

mongosh - $concat for one field in updateOne method

I want to update a particular document for the email field based on its id, but I don't want to overwrite the email field completely. Instead, I just want to add a string beside it (concatenate it with another string), I.e. I need the current value…
Normal
  • 1,616
  • 15
  • 39
0
votes
1 answer

MongoDB - Insert field names as string instead of getting the value

I want to create a new location field from already existing longitude and latitude. db.neigborhood.updateMany({}, { $set: { "location": { "type": "Point", "coordinates": ["$longitude", "$latitude"] } }}); I wrote this…
realPro
  • 1,713
  • 3
  • 22
  • 34
0
votes
1 answer

MongoDB - update an object inside a multi dimensional numerical indexed array

I am learming MongoDB (ver 5) [with Django (pymongo)] and facing a problem to update a multidimensional numerical array of objects. I want to update an object value nested within a multi dimensional array. Sample collection data: [ { "_id": { …
sariDon
  • 7,782
  • 2
  • 16
  • 27