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
0 answers

How to handle an 'lastModified' field in a mongodb collection?(In such a way that the field gets updated only if any value in the document is changed)

EDIT(15/04/2016): As per the comments received in this question I think there is no direct method in mongodb to do so. In that case is it possible to do this with MongooseJS? I am using mongodb, For one collection I need an 'lastModified' field,…
0
votes
1 answer

MongoDB Bulk.find.update() not modifying array objects, only adding new ones

I have a problem with MongoDB $set & $-operators. I try to modify an existing array My update method looks about like this (I cannot get the exact copy as it is written in Clojure/Monger): bulk.find({ _id: 2, …
Cihan Bebek
  • 408
  • 1
  • 5
  • 12
0
votes
1 answer

MongoDB: How to add or update array object depending if its found or not?

What mongoDB operation can I use to add a object to array, or, if object is already in array then update one of its values. Here is an example of my mongodb data: { "_id" : "1", "channelStatuses" : [ { "channel" :…
Cihan Bebek
  • 408
  • 1
  • 5
  • 12
0
votes
1 answer

How to remove or unset MongoSubdocument or field which is present at Multiple nested levels?

{ "_id":"zxxxxxx", "UserId":"xxxxxx", "MobileNum":"xxxxxxx", "Global":{ "count":2, "EventType":{ "MT":{ "count":2, "count_data":{ "20120302":2 }, …
Vinay Sawant
  • 368
  • 2
  • 7
  • 23
0
votes
2 answers

Mongodb Java nested update

I have a MongoDB document structure like this { "_id": "002", "list": [ { "year": "2015", "entries": [{...}, {...}] }, { "year": "2014", "entries": [{...}, {...}] } ] } I want to push a new element into…
FrankS
  • 1
0
votes
1 answer

mongodb update matched doc fail

There are 10000 docs import by mongoimport, the _id is 6 length random string {_id:"xxxxxx","u":0,"t":0} because mongoimport can not specifiy data type, so string like "123456" was imported as type int. So I manually delete originals and re-insert…
jean
  • 2,825
  • 2
  • 35
  • 72
0
votes
1 answer

Why the mongodb update method creates a new collection with the extension '_keys'?

I used the mongoDB method update to modify existing documents in a collection named annotations. My command, written in a Python script, is: db['annotations'].update({'_id': annotation['_id']}, {'$set': {'orthologs': orthologs}}, False) The field…
ldespons
  • 1
  • 1
0
votes
2 answers

mongodb update 1 million records

I have a collection with 1 million documents. Each document has an ip field. I have a node function which can return me the country code by passing ip as its param. I was going to get all the records, run my node function, insert the returned…
eded
  • 3,778
  • 8
  • 28
  • 42
0
votes
2 answers

MongoDB and Python: Multiple $push in same update. Only the last $push seems to be interpreted

I am crawling a some links on reddit and storing the results in a MongoDB collection. Here is the only line in my Python script that stores the data: reddit_links_collection.update( {'_id': id}, #…
elgehelge
  • 2,014
  • 1
  • 19
  • 24
0
votes
1 answer

Update a few fields in nested array in mongodb (with pymongo)

I am trying to update a few fields within an array within array An example document is like this: { id: 987654321 tweets: [ { text: "RT @947FreshFM: A vigil will be held for #SandyHook victims at UMd. at 7pm at Nyumburu Ampitheater.…
zephyr
  • 1
  • 1
0
votes
2 answers

MongoDB updating an Array Element by value matching

I have a collection 'locations', where I want to replace 'usa' with 'united+states'. The present state of the DB is as following: > db.locations.find({'country':'usa'}) { "_id" : "boston", "country" : [ "usa" ], "server" : "", "ts" :…
VaidAbhishek
  • 5,895
  • 7
  • 43
  • 59
0
votes
4 answers

Efficiently updating records in MongoDB / pymongo (perhaps in-place?)

I'm updating records in my collection like so: for document in myDB.find(): if compliesToSomeRule(document): myDB.update({'_id':document['_id']}, {'$set':{'something':'somevalue'}}) Is this the most efficient way to do it? It just seems…
LittleBobbyTables
  • 4,361
  • 9
  • 38
  • 67
0
votes
1 answer

Run 2 mongodb updates in 1 query based on a field value

I need to update a record in mongodb where the new value depends on the value of another field in the record. For instance given the following collection {id: 1, list: []} {id: 2, list: [6]} {id: 3, list: []} {id: 4, list: [6]} {id: 5, list: []} I…
ben39
  • 2,427
  • 4
  • 20
  • 19
-1
votes
1 answer

Move existing outer document to inner document

Given a collection of documents similar to the following document { "description": "janeetjack", "name": "Pocog bistro janeetjack" } where name is a unique field How do I update all existing documents and add additional fields so it looks like…
Bazinga_
  • 95
  • 1
  • 1
  • 5
-1
votes
1 answer

MongoDB update based on .map() array objects

So the issue is I have nested objects in a and array that no longer exist in the related collection. The collection (users) searched through looks like { "_id" : id, "locations" : [ { "_id" : id, …
Kuma
  • 1
  • 1
1 2 3
12
13