pymongo

Pymongo how to properly use $push to update an existing document

北城余情 提交于 2019-12-10 17:06:49
问题 I am doing something wrong or I don't understand $push (probably don't understand). Anyway I have the following document: { "_id" : ObjectId("501c83051d41c8753e000000"), "node" : "denver", "enc" : { "environment" : "production", "classes" : { "denver" : "" } }, "inherit" : "default" } And I am trying to make the document { "_id" : ObjectId("501c83051d41c8753e000000"), "node" : "denver", "enc" : { "environment" : "production", "classes" : { "denver" : "", "boulder" : ""} }, "inherit" :

BSON object size of document retrieved from DB

别等时光非礼了梦想. 提交于 2019-12-10 16:06:45
问题 Mongo shell have the bsonsize() method to get BSON size of a given document retrieved from DB. Is there any way of getting the same using PyMongo driver? I have found the bson module in the documentation but it is not fully clear to me how to use it to get the size of a document retrieved from DB. 回答1: Based on @SSDMS suggestion (thanks!) the following can be used: len(bson.BSON.encode(document)) I have tested with a couple of documents in my DB, comparing the result at mongo shell and with

Multiple field Indexing in pymongo

隐身守侯 提交于 2019-12-10 16:03:22
问题 I am trying to apply indexing for my mongodb collection through pymongo. I am using db[collection_name].ensure_index([("field_name" , "text"),("unique", 1), ("dropDups" , 1)]) and it works. But now how can apply it for multiple fields? Something like this db[collection_name].ensure_index([("field_name1" , "text"),("field_name2", "text"),("field_name3", "text"),("unique", 1), ("dropDups" , 1)]) I know we can use db.collection.ensureIndex({"$**":"text"},{"name":"TextIndex"}) in mongo shell but

Question about safe=True parameter for update operation of mongodb

淺唱寂寞╮ 提交于 2019-12-10 15:59:54
问题 Im working a mongodb database using pymongo python module. I have a function in my code which when called updates the records in the collection as follows. for record in coll.find(<some query here>): #Code here #... #... coll.update({ '_id' : record['_id'] },record) Now, if i modify the code as follows : for record in coll.find(<some query here>): try: #Code here #... #... coll.update({ '_id' : record['_id'] },record,safe=True) except: #Handle exception here Does this mean an exception will

How to close cursor in MongoKit

大城市里の小女人 提交于 2019-12-10 13:34:53
问题 I'm using MongoKit to perform iteration over a huge amount of data. During this process my cursor becomes invalid, and I'm getting OperationFailure: cursor id '369397057360964334' not valid at server I've read in mailing lists that I can pass parameter timeout=False to .find() method, but PyMongo FAQ says that I vave to take care of closing cursor myself. But I didn't find methods in MongoKit for that. Do I need to close cursor by hand, and if yes - how can I do it? 回答1: You'll have to close

How can I use mongo functions in pymongo?

孤人 提交于 2019-12-10 10:53:14
问题 I'm looking at mongodb tutorials. I want to make use of some javascript mongodb functions, but I can't see how I can do this with pymongo. I can fire up mongo shell, but this has to be done via pymongo as a part of my wsgi app. pymongo seems to have Code object - How can I 1)put some Code objects into my db, and 2)use them with pymongo? 回答1: Any JavaScript functions you want to run on the server have to be executed with "eval" command. You can see how to invoke that from Python here. You can

What is the relationship between flask, mongokit, pymongo, flask-pymongo?

荒凉一梦 提交于 2019-12-10 10:01:26
问题 I am really new to Flask and MongoDB . I read the Flask documentation about MongoDB , and turns out I felt so confused that I did not know which one I need to install. In Flask document, it says if you want to operate MongoDB , you should install Mongokit , yes I did, I build the user structure like document said, and save it, but it did not work, I look up into MongoDB shell, nothing stored there. So, i keep searching that I need Pymongo , and another document say that Flask-PyMongo is the

Does anyone know a working example of 2dsphere index in pymongo?

风流意气都作罢 提交于 2019-12-10 04:24:51
问题 I'm trying to code up a script to do basic 2dsphere index operation mentioned here 2dsphere using pymongo. I could not find any example to figure it out, this is my attempt so far: from pymongo import GEOSPHERE client=MongoClient('localhost',27017) db=client['dbtest'] points=db['points'] points.create_index([("loc",GEOSPHERE)]) points.insert({"loc":[2 5]}) points.insert({"loc":[30,5]}) more points.insert for doc in points.find({"loc" : {"$near": { "$geometry" : {"type":"Point","coordinates":

How can I return an array of mongodb objects in pymongo (without a cursor)? Can MapReduce do this?

扶醉桌前 提交于 2019-12-10 02:26:39
问题 I have a db set up in mongo that I'm accessing with pymongo. I'd like to be able to pull a small set of fields into a list of dictionaries. So, something like what I get in the mongo shell when I type... db.find({},{"variable1_of_interest":1, "variable2_of_interest":1}).limit(2).pretty() I'd like a python statement like: x = db.find({},{"variable1_of_interest":1, "variable2_of_interest":1}) where x is an array structure of some kind rather than a cursor---that is, instead of iterating, like:

import error: no module named bson

不羁的心 提交于 2019-12-10 01:12:13
问题 I'm trying to import json_util in my environment file: from bson import json_util I get this import error: no module named bson . I tried to pip install and uninstall pymongo and bson - but nothing seemed to help. I found out that the bson package is included in pymongo so I installed it explicitly and then I received an EPOCH_AWARE import error. Currently, only pymongo is installed. It works when I force the virtual environment by using this line: #subprocess.Popen(['/home/.virtualenvs