pymongo

What happens to a pymongo cursor when all its elements have been iterated?

无人久伴 提交于 2019-12-11 03:27:07
问题 I wanted to use pymongo to get an array of entries from my database. It seems to return a "cursor" instead. I don't know what that is. all_nodes = pymongo.MongoClient("mongodb://localhost")["provemath"]["nodes"].find(None) print('ALL NODES') for node in all_nodes: print(node) print('STILL NODES') for node in all_nodes: print(node) There output is: ALL NODES {'_notes': [], '_examples': [], '_type': 'definition', '_plural': None, '_counterexamples': [], '_intuitions': [], '_id': 'unique', '

with Python is there a way to listen for changes when insert or update is made in mongodb

百般思念 提交于 2019-12-11 03:25:46
问题 I am building a small system which throws data from a mongodb collection, it already works fine but I have to restart it everytime I make changes. I already have a monitor that dectect changes and restarts the server automatically but I want to do something like this with mongodb changes. I am currenlty using CentOs 5, Nginx, uWsgi & python2.7. 回答1: I'd look into using tailable cursors, which remain alive after they've reached the end of a collection, and can block until a new object is

ObjectID generated by server on pymongo

一个人想着一个人 提交于 2019-12-11 01:37:54
问题 I am using pymongo (python module for mongodb). I want the ObjectID to be created automatically by the server, however it seems to be created by pymongo itself when we don't specify it. The problem it raises is that I use ObjectID to sort by time (by just sorting by the _id field). However it seems that it is using the time set on each computer so we cannot truly rely on it. Any idea on how to solve this problem? 回答1: If you call save and pass it a document without an _id field, you can force

PyMongo Aggregate how to get executionStats

一笑奈何 提交于 2019-12-11 00:33:36
问题 I am trying to get executionStats of a Particular mongo aggregate query. I run db.command but that doesn't give "execution status" This is what I am trying to do. how to get Python Mongo Aggregate explain using db.command? pymongo aggregate don't allow explain option 来源: https://stackoverflow.com/questions/56253108/pymongo-aggregate-how-to-get-executionstats

Updating records in MongoDB through pymongo leads to deletion of most of them

◇◆丶佛笑我妖孽 提交于 2019-12-10 23:58:58
问题 Im working with a remote mongodb database in my python code.The code accessing the database and the database itself are on two different machines. The pymongo module version im using is 1.9+. The script consists of the following code: for s in coll.find({ "somefield.a_date" : { "$exists":False }, "somefield.b_date" : { "$exists":False }}): original = s['details']['c_date'] utc = from_tz.localize(original).astimezone(pytz.utc) s['details']['c_date'] = utc if str(type(s['somefield'])) != "<type

What is the difference between these two MongoDB queries?

混江龙づ霸主 提交于 2019-12-10 22:59:07
问题 Objective Find out possible differences in the following MongoDB queries and understand why one of them works and the other doesn't. Background A while ago I posted a questions asking for help regarding a MongoDB query: Using $push with $group with pymongo In that question my query didn't work, and I was looking for a way to fix it. I had a ton of help in the comments, and eventually found out the solution, but no one seems to be able to explain me why my first incorrect query doesn't work,

Mongoengine PointField gives location object expected, location array not in correct format error

試著忘記壹切 提交于 2019-12-10 19:12:30
问题 I have a model as follows: class Station(Document): location = PointField() Trying to write data as follows: station = Station() station.location = { "type": "Point", "coordinates": [ 81.4471435546875, 23.61432859499169 ] } station.save() However this gives the error Could not save document (location object expected, location array not in correct format) Mongoengine documentation says such a dictionary should be OK. What am I missing here? 回答1: I face similar problem once, in my case it

How fetch latest records using find_one in pymongo

拜拜、爱过 提交于 2019-12-10 19:05:33
问题 I have an existing program where I'm trying to fetch last inserted document which matches the key aws_account_id using find_one in pymongo. I'm using this query to perform the fetching: report = securitydb.scout.find_one({'aws_account_id': aws_account.account_number}) But this query returns a wrong document. The image below demonstrates the expected result and the wrong one that I'm getting. In the image, both documents have the same aws_account_id but the red one is inserted last. So the

pymongo sort() limit() different?

廉价感情. 提交于 2019-12-10 17:41:50
问题 1. db.bios.find().sort( { name: 1 } ).limit( 5 ) 2. db.bios.find().limit( 5 ).sort( { name: 1 } ) What is the different with them? They are equal? If first one doing: find all documents? it is bad. If db.bios.find().count() is very big(1000000), which process fast? what is find() default sequence? the insert sequence? Thanks. 回答1: 1.These two are equal, sorting will be done first, and then the result will be limited. 2.In order to optimize this, consider having index on name , if this is

Python, Convert bson output of mongodump to array of json objects (dictionaries)

本小妞迷上赌 提交于 2019-12-10 17:16:47
问题 I have dumped a mongodb collection using the mongodump command. The output is a dump directory which has these files: dump/ |___coll.bson |___coll.metadata.json How can I open the exported files to a array of dictionaries that work in python? I tried the following and none worked: with open('dump/coll.bson', 'rb') as f: coll_raw = f.read() import json coll = json.loads(coll_raw) # Using pymongo from bson.json_util import loads coll = loads(coll_raw) ValueError: No JSON object could be decoded