pymongo

Unable to serialize JSON object from MongoDB in python

冷暖自知 提交于 2019-12-06 07:56:28
I have the following JSON stored in my MongoDB: { "_id" : ObjectId("54fed786265e7f01d66ca778"), "id" : "http://some.site.somewhere/entry-schema#", "schema" : "http://json-schema.org/draft-04/schema#", "description" : "schema for an fstab entry", "type" : "object", "required" : [ "storage" ], "properties" : { "storage" : { "type" : "object", "oneOf" : [ DBRef("#/definitions/diskDevice", 1), DBRef("#/definitions/diskUUID", 2), DBRef("#/definitions/nfs", 3), DBRef("#/definitions/tmpfs", 4) ] } }, "definitions" : { "diskDevice" : { }, "diskUUID" : { }, "nfs" : { }, "tmpfs" : { } } } I have written

MongoDB Aggregation Limit Lookup

怎甘沉沦 提交于 2019-12-06 07:15:37
问题 I am using $lookup in PyMongo to successfully "join" two collections (this works). I am having a problem where the second collection I am joining in may exceed the BSON document size when it returns all of the records. I am looking to use $limit to limit the number of records that are allowed to join under "match_docs" eg: 100 records maximum from "comments" per obj_id: db.indicators.aggregate([ { "$lookup": { "from": "comments", "localField": "_id", "foreignField": "obj_id", "as": "match

Python对MongoDB增删改查

ⅰ亾dé卋堺 提交于 2019-12-06 07:11:21
pip install pymongo import pymongo # 建立连接 client = pymongo.MongoClient() # 指定数据库 (不存在则会新建) db = client['py_mongo'] # 删除数据库 # client.drop_database('py_mongo_temp') # 创建集合 # db.create_collection('col_temp') # 删除集合 # print(db.drop_collection('col_temp')) # 指定集合 (不存在则会新建) collection = db['mongo_col'] # collection = pymongo.MongoClient()['py_mongo']['mongo_col'] 基本使用 :insert() remove() update() find() # 增 insert() # 如果不指定_id参数,MongoDB会为文档分配一个唯一的ObjectId # 增加一条 # collection.insert({'_id':1,'name':'JiYu','num':0}) # 增加多条 # collection.insert( [ # {'name':'jiyu','num':12}, # {'name':'jiyu','num':34}, #

Book tagging with mongodb (many-to-many) implementation

萝らか妹 提交于 2019-12-06 06:51:54
Im trying to build a simple application in python, where I have tags that I associated to tags. Given the following data: Book: +-------------+--------------------------------+ | id | tags | +-------------+--------------------------------+ | 1 | [python, ruby, rails] | +-------------+--------------------------------+ | 2 | [fiction, fantasy] | +-------------+--------------------------------+ | 3 | [fiction, adventure] | +-------------+--------------------------------+ How would I (using pymongo) find: All books tagged as "fiction" All unique tags in the system 1. Find all books by tag: db

How to format data for MongoEngine PointField

柔情痞子 提交于 2019-12-06 05:56:33
So I wanted to do some experiments with location data in mongodb, so I wrote some python code to generate some testing data. Unfortunately the documentation at http://docs.mongoengine.org/apireference.html#mongoengine.fields.PointField isn't explicit about how to format the input. class Location(db.Document): coord = db.PointField(required=True) # GeoJSON Trying to store a list containing the lng/lat fails: >>> a = Location(coord=[1,2]) >>> a.save() mongoengine.errors.OperationError: Could not save document (location object expected, location array not in correct format) Passing a geoJSON

How can I use mongo functions in pymongo?

情到浓时终转凉″ 提交于 2019-12-06 05:49:13
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? Asya Kamsky 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 invoke functions you have stored on the server though it's not recommended. In general,

Iterating through PyMongo cursor throws InvalidBSON: year is out of range

空扰寡人 提交于 2019-12-06 05:28:21
I am using PyMongo to simply iterate over a Mongo collection, but I'm struggling with handling large Mongodb date objects. For example, if I have some data in a collection that looks like this: "bad_data" : [ { "id" : "id01", "label" : "bad_data", "value" : "exist", "type" : "String", "lastModified" : ISODate("2018-06-01T10:04:35.000Z"), "expires" : Date(9223372036854775000) } ] I will do something like: from pymongo import MongoClient, database, cursor, collection client = MongoClient('localhost') db = client['db1'] db.authenticate('user', 'pass', source='admin') collection = db['collection']

PyMongo doesn't iterate over collection

坚强是说给别人听的谎言 提交于 2019-12-06 04:48:34
问题 I have strange behaviour in Python/PyMongo. dbh = self.__connection__['test'] first = dbh['test_1'] second = dbh['test_2'] first_collection_records=first.find() second_collection_records=second.find() index_f=first_collection_records.count() //20 index_s=second_collection_records.count() //120 i=0 for f in first_collection_records: for s in second_collection_records: i=i+1 print i and it prints only 120 times (1..120) and not 20x120 times. Can someone tell me why it doesn't iterate through

pymongo - how to match on lookup?

泄露秘密 提交于 2019-12-06 03:14:34
I have two collections, a model and a papers collection. I need to be able to match fields from both of them. They have a field in common called reference which contains an identifier. I want to match documents that have the following 'authors' : 'Migliore M' from the papers collection 'celltypes' : 'Hippocampus CA3 pyramidal cell' from the models collection Here is what my code looks like: pipeline = [{'$lookup': {'from' : 'models', 'localField' : 'references', 'foreignField' : 'references', 'as' : 'cellmodels'}}, {'$match': {'authors' : 'Migliore M', 'cellmodels.celltypes' : 'Hippocampus CA3

MongoDB/PyMongo: How to use dot notation in a Map function?

你。 提交于 2019-12-06 01:50:05
问题 I'm trying to count how many records I have located in each zip code. In my MongoDB, zip code is embedded; using dot notation, it's located at a.res.z (a for address, res for residential, z for zip). For example, this works just fine: db.NY.count({'a.res.z' : '14120'}) But when I try the map function (in python, because I'm using PyMongo): map = Code("function () {" " emit(this.a.res.z, 1);" "}") I get this error when I call mapreduce: pymongo.errors.OperationFailure: db assertion failure,