0

Newbie in mongodb/python/pymongo. My mongodb client code works OK. Here it is :

db.meteo.find().forEach( function(myDoc) {
db.test.update({"contract_name" : myDoc.current_observation.observation_location.city},
                      { $set: { "temp_c_meteo" : myDoc.current_observation.temp_c ,
                             }
                      },
                      {upsert:false,
                      multi:true}) 
              })

this adds temp_c_meteo column to all documents in my test collection, when contract_name (from test collection) is equal to current_observation.observation_location.city (from meteo collection). And that's what i want!

But, i would like this to work the same in Python. Python 2.7-pymongo 2.6.3 is installed. But also Python 2.4.6 (64-bit) and 3.4.0 (64-bit).

Here is a part of the Python script :

[...]

saveInDB:

    #BE connected to MongoDB client
    MDBClient = MongoClient()
    db = MDBClient ['test']
    collection = db['infosdb']

[...]

meteos = collectionMeteo.find()
for met in meteos:
  logging.info('DEBUG - Update with meteo ' + met['current_observation']['observation_location']['city'])
  result = collection.update_many( {"contract_name" : met['current_observation']['observation_location']['city']},
    {
        "$set": {"temp_c_meteo" : met['current_observation']['temp_c']}
    })

And there is the error i get : ==> [06/21/2016 10:51:03 AM] [WARNING] : 'Collection' object is not callable. If you meant to call the 'update_many' method on a 'Collection' object it is failing because no such method exists.

I read this problem could be linked to some pymongo release ("The methods save and update are deprecated", that's why i try to use update_many) but not sure et how can i make this work? Thanks

Cop
  • 1

2 Answers2

0

Try using an attribute style access instead of dictionary style access?

Something like

db = MDBClient.test
collection = db.infosdb
Munosphere
  • 174
  • 1
  • 12
0

There is how i made it worked : no update_many but update.

[...]

saveInDB:

#BE connected to MongoDB client
MDBClient = MongoClient()
db = MDBClient.test
collection = db.infosdb
collectionMeteo = db.meteo

[...]

        try:
            meteos = collectionMeteo.find()
            for met in meteos:

                status = collection.find({"contract_name" : met['current_observation']
                ['observation_location']['city']})
                for stat in status:

                    result = collection.update(  {"_id" : stat['_id']},
                            {
                                "$set": {"temp_c_meteo" : met['current_observation']['temp_c']
                                        }

                                                     )                                                  
Cop
  • 1