pymongo

pymongo aggregate don't allow explain option

試著忘記壹切 提交于 2020-01-02 06:47:05
问题 I succesfully run: result = my_col.aggregate(my_pipeline, allowDiskUse=True) Now when I try: result = my_col.aggregate(my_pipeline, allowDiskUse=True, explain=True) it fails saying: pymongo.errors.ConfigurationError: The explain option is not supported. Use Database.command instead. Thus I try so as to add the needed option: result = mydb.command('aggregate', 'mycol', my_pipeline, {'explain':True}) but it fails saying: pymongo.errors.OperationFailure: 'pipeline' option must be specified as an

Why do I get a pymongo.cursor.Cursor when trying to query my mongodb db via pymongo?

天大地大妈咪最大 提交于 2020-01-02 05:36:45
问题 I have consumed a bunch of tweets in a mongodb database. I would like to query these tweets using pymongo. For example, I would like to query for screen_name. However, when I try to do this, python does not return a tweet but a message about pymongo.cursor.Cursor. Here is my code: import sys import pymongo from pymongo import Connection connection = Connection() db = connection.test tweets = db.tweets list(tweets.find())[:1] I get a JSON, which looks like this: {u'_id': ObjectId(

pymongo error: bson.errors.InvalidBSON: 'utf8' codec can't decode byte 0xa1 in position 25: invalid start byte

一笑奈何 提交于 2020-01-02 05:19:31
问题 tasks = list(self.collection.find().sort('_id',pymongo.DESCENDING).limit(1000)) I had a trouble when i use pymongo to solve a program. File "D:\Python27\lib\site-packages\pymongo-3.2.1-py2.7-win-amd64.egg\pymongo\cursor.py", line 1097, in next File "D:\Python27\lib\site-packages\pymongo-3.2.1-py2.7-win-amd64.egg\pymongo\cursor.py", line 1039, in _refresh File "D:\Python27\lib\site-packages\pymongo-3.2.1-py2.7-win-amd64.egg\pymongo\cursor.py", line 903, in __send_message File "D:\Python27\lib

Performance gain by using bulk inserts vs regular inserts in MongoDB

不羁岁月 提交于 2020-01-02 02:02:04
问题 What is the performance gain by using bulk inserts vs regular inserts in MongoDB and pymongo specifically. Are bulk inserts just a wrapper for regular inserts? 回答1: Bulk inserts are no wrappers for regular inserts. A bulk insert operation contains many documents sent as a whole. It saves as many database round trips. It is much more performant since you don't have to send each document over the network separately. 回答2: @dsmilkov Thats an advantage as you dont have to open a connection every

Pymongo API TypeError: Unhashable dict

醉酒当歌 提交于 2020-01-02 02:01:06
问题 I'm writing an API for my software so that it is easier to access mongodb. I have this line: def update(self, recid): self.collection.find_and_modify(query={"recid":recid}, update={{ "$set": {"creation_date":str( datetime.now() ) }}} ) Which throws TypeError: Unhashable type: 'dict' . This function is simply meant to find the document who's recid matches the argument and update its creation_date field. Why is this error occuring? 回答1: It's simple, you have added extra/redundant curly braces,

Compute first order derivative with MongoDB aggregation framework

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 00:52:15
问题 Is it possible to calculate a first order derivative using the aggregate framework? For example, I have the data : {time_series : [10,20,40,70,110]} I'm trying to obtain an output like: {derivative : [10,20,30,40]} 回答1: db.collection.aggregate( [ { "$addFields": { "indexes": { "$range": [ 0, { "$size": "$time_series" } ] }, "reversedSeries": { "$reverseArray": "$time_series" } } }, { "$project": { "derivatives": { "$reverseArray": { "$slice": [ { "$map": { "input": { "$zip": { "inputs": [ "

Want to Update Array of Array Object Value in Mongo

我的未来我决定 提交于 2020-01-01 18:04:34
问题 I have the following MongoDB document: { "_id": ObjectId(), "sku": "V4696-DR-V33", "options": [ { "sku": "8903689984338", "stores": [ { "code": "AND1", "zipcode": "110070", "inventory": -1000 }, { "code": "AND2", "zipcode": "201010", "inventory": -1000 }, { "code": "AND3", "zipcode": "411001", "inventory": -1000 }, { "code": "AND4", "zipcode": " 700020", "inventory": -1000 }, { "code": "AND5", "zipcode": "110015", "inventory": -1000 } ], "price": 2199, "_id": ObjectId(), "size": "14" }, {

MongoDB shell's db.stats() in php and python

天涯浪子 提交于 2020-01-01 09:05:31
问题 I can see the statistics from Mongo shell as db.stats() or db.collection_name.stats() How do I view statistics of a database, or of a collection, from PHP and Python. EDIT : I have done it in PHP but still cant do it in Python. Help? 回答1: This is how you do it in Python if you are using the PyMongo driver: connection = pymongo.Connection(host = "127.0.0.1", port = 27017) db = connection["test_db"] test_collection = db["test_collection"] db.command("dbstats") # prints database stats for "test

Blueprints, PyMongo in Flask

左心房为你撑大大i 提交于 2020-01-01 08:53:10
问题 What is the correct way of picking up my mongo object in my Blueprints? Here is how I have my parent login.py : app.config.from_object('config') from flask.ext.pymongo import PyMongo from child import child from child2 import child2 app = Flask(__name__) app.register_blueprint(child2.child2) app.register_blueprint(child.child) in my child.py from app import app from flask.ext.pymongo import PyMongo mongo = PyMongo(app) child = Blueprint('child', __name__) child2.py is the same structure as

Invalid operator '$size' in aggregation

烈酒焚心 提交于 2019-12-31 05:28:13
问题 I got the following piece of code: from pymongo import MongoClient client = MongoClient('ipOfServer') db = client.admin db.authenticate('login', 'password', source='admin_') heh = list(db.events.aggregate( [ {"$match": {"status": 'start'}}, {"$group": {"_id": "$eventName", "players": {"$addToSet": "$uid"}}}, {"$project": {"_id": 1, "Count": {"$size": "$players"}}} ])) print(heh) and this is worked for the original programmer who wrote and tested it code result while testing. But when I try to