pymongo

How to achieve a read only connection using pymongo

我怕爱的太早我们不能终老 提交于 2019-12-11 07:13:59
问题 How to achieve a read-only connection to the secondary nodes of the MongoDB. I have a primary node and two secondary nodes. I want a read-only connection to secondary nodes. I tried MongoReplicaSetClient but did not get what I wanted. Is it possible to have a read-only connection to primary node? 回答1: You'll want to specify a Read Preference on your queries. A read preference of Secondary Preferred will send queries to a Secondary node but will fall back to the Primary in the event that a

Python, Django, Mongodb: ImportError: No module named bson.objectid [duplicate]

笑着哭i 提交于 2019-12-11 06:44:20
问题 This question already has answers here : ImportError: No module named objectid (4 answers) Closed 6 years ago . I'm following this tutorial exactly. Tried deleting and restarting many times with virtualenv and I'm still getting errors. Is it python, mongodb and django supposed to be this frustrating to set up? http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-django-mongodb-engine/ I get a problem when I try to call post.save() I then get this error: Traceback (most

{ $t: “”, $v: “”} structure in MongoDB collections using pymongo

半腔热情 提交于 2019-12-11 06:26:51
问题 I'm using pymongo libraries to upload my data into documentDB (it accepts MongoDB libraries). My json data objects (read using the json library from a json file) before sending them to my documentDB collection are in the format of (a portion of the data): ... { "id": [ "{}", "i" ] } ... However, in the database they are stored like: ... { "id": { "$t": 4, "$v": [ { "$t": 2, "$v": "{}" }, { "$t": 2, "$v": "i" } ] } } ... My python code that inserts data into MongoDB is client = MongoClient(

Average Aggregation with String Timestamp

醉酒当歌 提交于 2019-12-11 06:25:24
问题 I have records in database as follow : { "_id" : ObjectId("592d4f43d69b643ac0cb9149"), "timestamp" : "2017-03-01 17:09:00", "Technique-Meteo_Direction moyenne du vent_Mean value wind direction[]" : 0.0, "Technique-Meteo_Précipitations_Precipitation status[]" : 0.0, "Technique-Meteo_Direction du vent_Wind direction[]" : 0.0 } { "_id" : ObjectId("592d3a6cd69b643ac0cae395"), "timestamp" : "2017-01-30 09:31:00", "Technique-Electrique_Prises de Courant_Power1[W]" : 14.0, "Technique-Electrique

PYTHON - PYMONGO - Invalid Syntax with $or

隐身守侯 提交于 2019-12-11 06:07:20
问题 I can't seem to get this to work with pymongo it was working before I added the $or option. Am I missing something obvious with this dataout = releasescollection.find( { $or: [{"l_title":{"$regex": "i walk the line", "$options": "-i"}}, {"artistJoins.0.artist_name":{"$regex": "Johnny Cash", "$options": "-i"}}]}).sort('id', pymongo.ASCENDING).limit(25) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "discogs.py", line 51 dataout = releasescollection.find( { $or: [{

Is there a way to bind another function like limit() to pymongo query based on condition?

孤人 提交于 2019-12-11 05:53:47
问题 I have a query which is as below: def get_data(self, limit=None): # I just want to add limit() in case it is set, not otherwise self.collection.find(condition) self.collection refers to the collection in the class. If limit parameter is set I need to bind limit() pymongo function to the query. NB: the limit is just a sample case it could be any function like sort , etc. So I just want to know is there a mechanism to bind params like this? 回答1: Do you mean like this?: def get_data(self, limit

What data 'structure' does fs.get_last_version return?

一笑奈何 提交于 2019-12-11 05:08:05
问题 When I use get_last_version to get an image from the database, what is actually returned ie an array, the merged binary data of all the chunks that make up the file (as a string), or something else? dbname = 'grid_files' db = connection[dbname] fs = gridfs.GridFS(db) filename = "my_image.jpg" my_image_file = fs.get_last_version(filename=filename) I'm wanting to base64 encode my_image_file with: import base64 encoded_img_file = base64.b64encode(my_image_file) return encoded_img_file But I'm

How to correctly query a MongoDB nested document with python?

十年热恋 提交于 2019-12-11 04:29:17
问题 Background: I just learned the basics of databases and mongo this week. I have a collection named 'orderstest' of 10K documents with the following basic form: { "_id": { "$oid": "2309823082039482" }, "Order": "12345678920000", "Client": "Client Name Inc.", "OrderUId": "3452-2342-9393-0100", "OrderItems": [ { "Client": "Client Name Inc.", "Details": [ { "Key": "EnterpriseCode", "Value": "XYZ1000" }, { "Key": "AWSRegion", "Value": "Frankfurt" }, ], "ProductUId": "A90", "OrderItemUId": "ABC1000"

PyMongo misbehaving with floating point numbers

五迷三道 提交于 2019-12-11 03:56:33
问题 I am using PyMongo to save data to a MongoDB. THe following is my code: from pymongo import MongoClient import time class data2db: def __init__(self): pass def enter_data(self, data): client = MongoClient('127.0.0.1', 27017) db = client.db coll = db.Temperature1 post = {"auth": data, "Time": time.asctime(time.localtime(time.time()))} post_ = coll.insert(post) c = data2db() c.enter_data(24.3) However, when I retrieve this object I get: { "_id" : ObjectId("558019749f43b8c19779c106"), "auth" :

inserting items in list in mongodb document

ε祈祈猫儿з 提交于 2019-12-11 03:39:12
问题 I have a document like this { "_id" : "decfed9a04b997d", "pushed_list" : [ ] } and I want to update/insert "pushed_list" by these sub items stored in a python list lis1 = [ { item : 'item1', desc : 'desc_item1' }, { item : 'item2', desc : 'desc_item2' }, { ........................ } ] I tried this db.monitor.update({'_id' : 'dbjkdd'}, { '$push':{'pushed_list': {'$each':{lis1}}}}) Its giving me an error. Traceback (most recent call last): File "monitor.py", line 13, in <module> db.monitor