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

Set same value on multiple fields

I have the following document structure: { ... timings: { mon: "", tue: "", wed: "", thu: "", fri: "", sat: "", sun: "", } ... } I want to set the value "Open 24 Hours" for all…
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
3
votes
1 answer

mongodb: java: How to update a field in MongoDB using expression with existing value

I need to update a field using some expression. Given a DBObject query, how can I multiply or divide given field by an argument value? How can I write a function performing operation something like this, but in Java: function(DBObject queryObject,…
N D Thokare
  • 1,703
  • 6
  • 35
  • 57
3
votes
2 answers

MongoDB/PyMongo won't $set attribute to document - but sets all other attributes! (bizarre error)

I'm trying to write a defaultdict variable to a document in my MongoDB. Everything else sets fine, just not this one attribute, its bizarre! I'm setting a rather large defaultdict called 'domains', which has worked many times before. Check out this…
LittleBobbyTables
  • 4,361
  • 9
  • 38
  • 67
2
votes
1 answer

MongoDB: How to remove a property from all objects in array?

I want to unset a property from all items in an array of objects in MongoDB. Playground Link Explanation: Suppose I have an array of objects with the following structure in a document: { "key": 1, "questions": [ { "text":…
Chique
  • 735
  • 3
  • 15
2
votes
1 answer

MongoDB - Update nested array with many nested objects

I need to update the dateP in the following structure with "2022-01-02", but it seems not an easy task: { "_id": ObjectId("5c05984246a0201286d4b57a"), "_a": [ { "_p": { "s": { "a": {} } } }, { …
R2D2
  • 9,410
  • 2
  • 12
  • 28
2
votes
1 answer

MongoDB - Cannot create field 'ChildProperty' in element {ParentProperty: is null}

I have a User class: public class User { public Guid? Id { get; set; } public String? Name { get; set; } public Address? Address { get; set; } } and Address class: public class Address { public String? Street { get; set; } …
ScubaSteve
  • 7,724
  • 8
  • 52
  • 65
2
votes
2 answers

MongoDB : How to update a new field value according to existing field value in each document?

I am working on a project and it currently goes through each document to edit the field and lags a lot. { "Name" : "Susie" , "This Semester" : 10 , "Last Semester" : 0 } { "Name" : "John" , "This Semester" : 20 , "Last Semester" : 0 } ... I have…
2
votes
1 answer

Find and replace in array of strings in mongodb

I'm using MongoDB v4.4. My document structure looks like this: { _id: ObjectId("..."), photos: ["image1.png", "image2.png"] }, { _id: ObjectId("..."), photos: ["image3.png", "another_image.png, image5.jpg"] }, { _id: ObjectId("..."), …
2
votes
1 answer

How to move MongoDB document fields to an array of objects?

Given a collection of documents similar to the following document { "_id": { "$oid": "60582f08bf1d636f4b762ebc" } "experience": [{ "title": "Senior Customer Success Account Manager", "company": "Microsoft", …
2
votes
1 answer

Update MongoDB collection by matching _id

I am trying to update mongodb collection from Python using condition on _id Like if I found match of _id in python dataframe I need to update corresponding document in colllection Below script is working, but it takes time for exeution if there are…
user3734568
  • 1,311
  • 2
  • 22
  • 36
2
votes
1 answer

How do I remove elements not matching conditions with MongoDB $pull?

I have a MongDB document that looks like this: { values: [{val:true}, {val:false}, {val:true}, {val:"dgfdshfsj"}] } How would I use the MongoDB $pull operator to remove all elements from the array which are not true, somewhat like…
Isaac Krementsov
  • 646
  • 2
  • 12
  • 28
2
votes
2 answers

Why does my update $set code remove my entire document?

Help! I don't what am doing wrong, when I try to update an existing field using the $set method the entire document gets removed. Can you kindly point out what I am doing wrong in my code: recipientsDetails.update({_id: "GCYmFqZbaaYD7DvMZ"}, {$set:…
SirBT
  • 1,580
  • 5
  • 22
  • 51
2
votes
1 answer

How to update a key value pair inside mongodb structure

I have a mongodb collection where objects are structured as such: { "id": "1234", "history": [ { "userid": 100, "myobjects": [{id, id1, id4}] }, { "userid": 200, "myobjects": [{id2, id3, id5}] }, } I'm…
Anne Rose
  • 47
  • 4
2
votes
1 answer

node.js mongodb only update first found subdocument in array

a User collection: { "_id" : ObjectId("5785d10570d6c39923d476cf"), "name" : "BB Cafe", "transaction" : [ { "id" : "m69bkn", "type" : "TYPE1", "amount" : 0, }, { "id" :…
Mark Thien
  • 1,108
  • 2
  • 22
  • 35
2
votes
0 answers

multi update only works with $ operators in mongodb,Replace document Entirely

I want to replace some document entirely with multi:true option. db.user.find().pretty(); { "_id" : ObjectId("5799b106d15203df993935d9"), "name" : "zhangyun", "age" : 33 } { "_id" :…
MrJ
  • 33
  • 1
  • 7
1
2
3
12 13