pymongo

Best way to read and update mongodb documents using pymongo

筅森魡賤 提交于 2019-12-23 08:35:51
问题 iam trying to read a mongodb collection document by document in order to fetch every record encrypt some of fields in the record and put it back to database. for record in coll.find(): #modifying record here coll.update(record) this is causing a serious problem i.e already updated documents are read again by cursor and same document is processed again in loop (same document is trying to update again) hope this may be one of the solution to the problem. list_coll = [record for record in coll

How to mock mongodb for python unittests?

痴心易碎 提交于 2019-12-23 07:16:10
问题 I am using mock module for Python 2.7 to mock my other functions and using unittest for writing unit tests. I am wondering if mocking the MongoDB is different than using mock functionality ( mock.patch a function that is being called?) Or I need to use another different package for that purpose? I do not think I want to have a test mongodb instance running. All I want is some tempo data and being able to call pymongo functionality. I am just a bit lost in thinking of is there a way to write a

How to mock mongodb for python unittests?

我的未来我决定 提交于 2019-12-23 07:14:31
问题 I am using mock module for Python 2.7 to mock my other functions and using unittest for writing unit tests. I am wondering if mocking the MongoDB is different than using mock functionality ( mock.patch a function that is being called?) Or I need to use another different package for that purpose? I do not think I want to have a test mongodb instance running. All I want is some tempo data and being able to call pymongo functionality. I am just a bit lost in thinking of is there a way to write a

way to update multiple documents with different values

一个人想着一个人 提交于 2019-12-23 06:53:38
问题 I have the following documents: [{ "_id":1, "name":"john", "position":1 }, {"_id":2, "name":"bob", "position":2 }, {"_id":3, "name":"tom", "position":3 }] In the UI a user can change position of items(eg moving Bob to first position, john gets position 2, tom - position 3). Is there any way to update all positions in all documents at once? 回答1: You can not update two documents at once with a MongoDB query. You will always have to do that in two queries. You can of course set a value of a

mongoimport CSV with pymongo

我的梦境 提交于 2019-12-23 06:26:46
问题 I found a command for mongodb which lets you import CSV files as a whole: https://docs.mongodb.com/manual/reference/program/mongoimport/ like this: mongoimport --db users --type csv --headerline --file /opt/backups/contacts.csv However, there seems to be no mention of this in Pymongo. Does it exists, and if so what is the syntax for it? Thanks in advance 回答1: mongoimport is a command-line program that you install. Instructions for installing MongoDB, including its tools like mongoimport , are

pymongo syntax to update a subdocument

你离开我真会死。 提交于 2019-12-23 03:43:09
问题 My documents look like this: { a: "..." subdocs [ { l: "..." m: "..." n: 0 }, { l: "..." m: "..." n: 0 } } } I have to update the 'n' field in a particular subdoc using pymongo. I have the document and the index of the subdocument so I can get the subdoc like this subdoc = mydoc['subdocs'][index] I try to do an update through pymongo coll.update( { mydoc['subdocs'][index] : subdoc }, { "$inc": { n: 1 }} ) I get this exception <type 'exceptions.TypeError'> I've tried several variations on this

Using pymongo's ReplicaSetConnection: sometimes getting “IndexError: no such item for Cursor”

旧巷老猫 提交于 2019-12-23 03:39:14
问题 I started using pymongo's (version 2.2.1) ReplicaSetConnection object instead of the pymongo.Connection object. Now, when I perform reads from the database, like: if cur.count() == 0: raise NoDocumentsFound(self.name, self.COLLECTION_NAME) elif cur.count() > 1: raise TooManyDocumentsFound(self.name, self.COLLECTION_NAME) cur.rewind() rec = cur[0] I sometimes receive an " IndexError: no such item for Cursor instance " on the final line. From all I can find out about this error, it should occur

Pymongo return values only as list

和自甴很熟 提交于 2019-12-23 03:37:07
问题 The following pymongo query gives me all the values I need: l=list(db.rounds.find({"current_strategy":"PPStrategy4016"},{"myFundsChange":1,"_id": 0})) {'myFundsChange': '-0.30000000000000004'}, {'myFundsChange': '0.0'}, {'myFundsChange': '0.0'}, {'myFundsChange': '-0.040000000000000036'}, {'myFundsChange': '-0.08000000000000007'}, {'myFundsChange': '-0.20999999999999996'}, {'myFundsChange': '-0.47'}, {'myFundsChange': '0.0'}, {'myFundsChange': '0.0'}, {'myFundsChange': '-0.040000000000000036'

pymongo - Unable to connect to mongodb running on EC2

南笙酒味 提交于 2019-12-23 03:02:17
问题 I am connecting to a mongodb server on EC2. The mongo collections require authentication to connect. I tried everything but I am getting the following error and can't seem to correct it. from pymongo import MongoClient mongo_username = "username" mongo_password = "password" ssh_user = "user" ssh_address = "ec2-**********.amazonaws.com" ssh_port = 22 private_key = "path/to/key/mykey.pem" def connect_to_mongo(): try: client = MongoClient("mongodb://"+mongo_username+":"+mongo_password+"@" + ssh

Deploying Flask in Openshift

假如想象 提交于 2019-12-23 02:55:16
问题 The following codes are working without any problem in my system's localhost... But ain't doing the job on OpenShift.. There is something wrong with my wsgi.py .. Do I have to pass my username and password using environment variables OR I've need to change the localhost ? The following is the tree of the directory/repository... myflaskaws ├── requirements.txt ├── setup.py ├── static │ ├── assets │ │ ├── style.css │ └── images │ ├── no.png │ └── yes.png ├── templates │ ├── index.html │ ├──