pymongo

Query tangled array in Pymongo

大城市里の小女人 提交于 2019-12-11 16:11:51
问题 I am trying to query a very tangled collection. The schema: {'tags': {'variables': [{'value': '3x9', 'var_name': 's'}, {'value': '12:00AM', 'var_name': 'x'}, {'value': 'goog', 'var_name': 'y'}]}, 'url': 'https://www.google.com'}] The Query: res = mycol.aggregate([{"$unwind":"$tags"} ,{'$match': {'tags.tag.name':'A A', 'aR': 98765}} ,{'$project': {'url': 1, 'tags.vars': 1, '_id': 0}},]) res = list(res) for x in res: print(res) The Result: {'tags': {'variables': [{'value': '3x9', 'var_name': 's

MongoDB optimize multiple find_one + insert inside loop

时光毁灭记忆、已成空白 提交于 2019-12-11 15:14:29
问题 I'm using MongoDB 4.0.1 and Pymongo with pyhton 3.5. I have to loop over 12000 items every 30 - 60 seconds and add new data into MongoDB. For this example we will talk about User, Pet and Car. The User can get 1 Car and 1 Pet. I need the pet ObjectID and the car ObjectID to create my User so I have to add them one by one in the loop and this is very slow. It takes ~25 seconds to find existing data and add them if the data not exist. while dictionary != False: # Create pet if not exist

how to get Python Mongo Aggregate explain using db.command?

房东的猫 提交于 2019-12-11 14:18:49
问题 Starting with MongoDB 3.2 explain() uses the default verbosity mode of the explain command, allPlansExecution. To use a different verbosity use command() to run the explain command directly. My aggregate query looks something like this collection.aggregate(pipeline=QUERY, explain=True) This returns queryPlanner verbose and not executionStats . What mongo shell supports? db.Item_Process_For_Report.explain("executionStats").aggregate([]) What am I trying to do? db.command('explain', <What goes

Return .str of ObjectID using pymongo

六月ゝ 毕业季﹏ 提交于 2019-12-11 14:15:21
问题 How would I return just the string component of an BSON ObjectId using pymongo. I'm able to encode a string into an Object id by importing ObjectId from bson.objectid; but am unable to do the reverse. When I try: for post in db.votes.find({'user_id':userQuery['_id']}): posts += post['_id'].str I get an ObjectId has no attribute str error. Thanks! 回答1: The standard way in python to get object's string representation is using the str builtin function: id = bson.objectid.ObjectId() str(id) =>

Pymongo permissions issue for safe inserts

时光总嘲笑我的痴心妄想 提交于 2019-12-11 13:56:55
问题 I have an instance of Mongo running and can connect and authenticate successfully to a database. I can bulk insert records using collection.insert([list of records to insert]) . However, when I add safe=True to ensure that the records are inserted, like the following command, I get the error below, which seems like a permissions issue. How can I fix this? collection.insert(records_to_insert, safe=True) File "/.../python2.6/site-packages/pymongo/collection.py", line 270, in insert check_keys,

Insert Pandas Timestamp into Mongodb

寵の児 提交于 2019-12-11 13:18:40
问题 I'm trying to insert a Pandas DataFrame into Mongodb using PyMongo. df.head() Because the index if the Pandas DataFrame is a DatetimeIndex , converting the DataFrame to a dict and inserting it to Mongodb: db.testCollection.insert( df.T.to_dict() ) gives rise to an error: InvalidDocument: documents must have only string keys, key was Timestamp('2016-04-07 09:30:00') How can we convert the DatetimeIndex to something else that can be inserted into Mongodb, and later still be able to be converted

How do I tell if I inserted during upsert in mongoengine without (the deprecated) get_or_create?

蓝咒 提交于 2019-12-11 13:06:36
问题 I need to know if an upsert inserted, using MongoEngine (or if necessary, pymongo). The code looks like this: ret = MyMongoCollection.objects(name=desiredname) .update_one( { upsert: True, field1: 2 } ) Mongoengine seems to return only "num_affected" which is always exactly 1 here, by definition. I know these exist, but I'm looking for the Python flavor. How to do "insert if not exist else update" with mongoengine? Check if MongoDB upsert did an insert or an update 回答1: Here's another answer

Querying for null field using pymongo

て烟熏妆下的殇ゞ 提交于 2019-12-11 09:05:19
问题 I would like to query for a null field in mongo using python, however it struggles to deal with the words null or false. it either gives me the error that they are undefined in python or it searches for the string null and false in mongo, neither of which I want to happen. collection = pymongo.MongoClient('mongodb://localhost:27017/')['historical'].highjump 1) data = pd.DataFrame(list(collection.find({"min":"null"}))) 2) data = pd.DataFrame(list(collection.find({"min":null}))) 3) data = pd

pymongo installed but import fails

此生再无相见时 提交于 2019-12-11 08:25:38
问题 CentOS 5.8 ships with Python 2.4.3. I installed pymongo using command: sudo pip install pymongo (after installing pip with easy_install after installing python-pip...typical CentOS, nothing works out of the box). The install appears to work, I get the messages: Successfully installed pymongo Cleaning up... Then, when I run import pymongo, I get this: ImportError: No module named pymongo I followed the standard pymongo install procedure so pymongo definitely doesn't work on CentOS. Anybody

Unique index into a subdocument list, indexed by one key

狂风中的少年 提交于 2019-12-11 07:43:22
问题 i need to know if is possible to have a list of objects, where the objects are uniques by day. I have a collection with this format: { domain: "google.com" counters: [ { day: "2011-08-03", metric1: 10, metric_2: 15 } { day: "2011-08-04", metric1: 08, metric_2: 07 } { day: "2011-08-05", metric1: 20, metric_2: 150 } ] } I tried something like that: db.test.ensureIndex({ domain: 1, 'counters.day': 1 }, { unique: true }). with upsert and $push , but this not works. Then I tried with upsert and