1

I have 10 documents in a collection:

>> {'_id':0, 'self':{....}} {'_id':1,'self':{....}} ........ {'_id':9,'self':{....}}

now I want to update all documents with their count, which means after the updating, the documents will look like:

>> {'_id':0, 'count':0, 'self':{....}} {'_id':1,'count':1, 'self':{....}} ........ {'_id':9,'count':9,'self':{....}}

I know there is a update({},{'$set':{}}) method but I don't know how to use this to update different values for each document. Does anyone know some fast methods to do this?

styvane
  • 59,869
  • 19
  • 150
  • 156
gladys0313
  • 2,569
  • 6
  • 27
  • 51

4 Answers4

0

Suppose you have this data structure in database:

{

"_id" : ObjectId("4f9808648859c65d"),

"array" : [

{"text" : "foo", "value" : 11},

{"text" : "foo", "value" : 22},

{"text" : "foobar", "value" : 33}

]

}

You can update specific value using following query:

db.foo.update({"array._id" : 1}, {"$set" : {"array.$.text" : "blah"}})

I hope this will be helpful for you. Let me know.

itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
jatinkumar patel
  • 2,920
  • 21
  • 28
0

I don't know if it's fast enough but you can write a script to do that and execute with eval.

Example:

import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.test_database
db.eval("var count = 0; db.collection_name.find().forEach(function(obj) { obj.count = count; db.collection_name.save(obj); count++ })")
Dimas Kotvan
  • 723
  • 1
  • 7
  • 10
0
for index,each in enumerate(myCollection.find({})):
    each["count"] = index
    id = each["_id"]
    each.pop("_id", None)
    collection.update({"_id":id},{"$set":each},upsert=True)

Note : documents are un-ordered in a collection as mentioned by @Michael

Heisenberg
  • 1,500
  • 3
  • 18
  • 35
  • Ah, thanks for your answer, so if the index value is important for each document (i.e., it makes sense if the documents are in order), then it's useless for me to assign the index value, right? – gladys0313 Apr 19 '15 at 09:02
  • @gladys0313 yes it is – styvane Apr 19 '15 at 09:04
  • @Heisenberg cool! Just want to ask more: I can use sort() to sort my documents and then the index will make sense, right – gladys0313 Apr 19 '15 at 09:09
  • @gladys0313 what is your goal? – styvane Apr 19 '15 at 09:14
  • @gladys0313: what is the significance of `count` in your application ? – Heisenberg Apr 19 '15 at 09:31
  • @Michael, each document in my collection represents a city grid and the whole collection is a whole city. I generate the documents from left bottom to top right of the city and I want the index to follow this order – gladys0313 Apr 19 '15 at 09:40
  • @Heisenberg, each document in my collection represents a city grid and the whole collection is a whole city. I generate the documents from left bottom to top right of the city and I want the index to follow this order – gladys0313 Apr 19 '15 at 09:40
0

Assuming you are running on mongodb shell

> db.docs.find().forEach(
    function (elem) {
        db.docs.update(
            {
                _id: elem._id
            },
            {
                $set: {
                    count: elem._id
                }
            }
        );
    }
);
Syed Mauze Rehan
  • 1,125
  • 14
  • 31
  • Reference http://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field http://stackoverflow.com/questions/3788256/mongodb-updating-documents-using-data-from-the-same-document/3792958#3792958 – Syed Mauze Rehan Apr 19 '15 at 11:38