pymongo

Alternative for Pymongo cursor iteration

跟風遠走 提交于 2019-12-14 03:59:38
问题 I went through many cursor iterationn questions, but saw nothing which can solve my problem. I have a database of the form [{book:'A', author:'John'}, {book:'B', author:'Tony'}, {book:'C', author:'John'}...] Multiple books for same author possible. What I need is 2 arrays authors = ['John','Tony','John'] books = ['A','B','C'] where corresponding elements falls at same index in both arrays. Now I am getting it by cursor iteration. authors =[] books =[] cursor = collection.find() for elem in

Why am I getting TypeError: 'module' object is not callable in python?

不打扰是莪最后的温柔 提交于 2019-12-14 02:43:21
问题 I'm creating a string that I an then use in a method that queries a mongodb collection. Eventually the dates will be from user input. Here's the relevant code and string: import pymongo from pymongo import MongoClient from datetime import datetime import time import datetime start_yr = 2015 start_mnth = 2 start_day = 1 end_yr = 2015 end_mnth = 2 end_day = 28 # this is the line called in the error created_at_string = { "created_at": {"$gte" : datetime(start_yr, start_mnth, start_day),"$lt" :

Can't Return JSON object using MongoEngine Pymongo with Django?

北慕城南 提交于 2019-12-14 00:22:37
问题 So I'm trying to return a JSON object for a project. I've spent a few hours trying to get Django just returning the JSON. Heres the view that we've been working with: def json(request, first_name): user = User.objects.all() #user = User.objects.all().values() result = simplejson.dumps(user, default=json_util.default) return HttpResponse(result) Here's my model: class User(Document): gender = StringField( choices=['male', 'female', 'Unknown']) age = IntField() email = EmailField() display_name

Get string array from Pymongo query

核能气质少年 提交于 2019-12-13 21:29:34
问题 I need to get an array with the values from the field 'colname'. I can't return a Cursor, just the array of values. Is there a way to query this array without having to loop the Cursor? I feel this is a waste of processing resources. Right now I'm doing this: from pymongo import MongoClient client = MongoClient('mongodb://localhost:27017/') headers = client['headers'] entomo = headers.entomo entomo_data = entomo.find() entomo_array = [] for data in entomo_data: entomo_array.append(data[

Pymongo find value in subdocuments

大憨熊 提交于 2019-12-13 18:35:52
问题 I'm using MongoDB 4 and Python 3. I have 3 collections. The first collection got 2 referenced fields on the other collections. Example : User { _id : ObjectId("5b866e8e06a77b30ce272ba6"), name : "John", pet : ObjectId("5b9248cc06a77b09a496bad0"), car : ObjectId("5b214c044ds32f6bad7d2"), } Pet { _id : ObjectId("5b9248cc06a77b09a496bad0"), name : "Mickey", } Car { _id : ObjectId("5b214c044ds32f6bad7d2"), model : "Tesla" } So one User has one car and one pet. I need to query the User collection

How to index unknown fields of a known field in PyMongo?

ぃ、小莉子 提交于 2019-12-13 18:28:44
问题 I am trying to find unique words in millions of tweets and also I want to keep where each word appears. In addition to that, I am also grouping the words by their initial. Here is a sample code: from pymongo import UpdateOne # connect to db stuff for word in words: # this is actually not the real loop I've used but it fits for this example # assume tweet_id's and position is calculated here initial = word[0] ret = {"tweet_id": tweet_id, "pos": (beg, end)} # additional information about word

installation of pymongo works but fails at import in python3

孤者浪人 提交于 2019-12-13 12:28:07
问题 I'm currently running Ubuntu 12.10 and tried to get pymongo to work properly under python3. Things I have tried: 1. apt-get install python-pymongo 2. python setup.py install #from git source 3. easy_install pymongo 4. easy_install pymongo3 5. pip install pymongo 6. pip install pymongo3 #needed a fix in the download script I have also removed and cleaned between the installations as best as I could. If I import pymongo: In [1]: import pymongo ---------------------------------------------------

Running multiple queries in mongo`

杀马特。学长 韩版系。学妹 提交于 2019-12-13 09:16:33
问题 I have a collection and want to get a set of results that met a set of conditions. I understand the Mongo doesn't let you use joins so I would need to run separate queries and join the results into a single response. But is it possible to join the results of separate queries together to get the intended output. Are there any basic examples I could see query results joined together. Thanks For example could I join these two queries so I get the results of both queries: coll.find({"coordinates

How can I bulk insert with MongoDB (using PyMongo), even when one record of the bulk fails?

被刻印的时光 ゝ 提交于 2019-12-13 07:12:15
问题 I have some Python code that uses PyMongo to insert many lists (of 1000 objects each), into a collection with a unique index (field name is data_id ). However, some of my lists of objects have duplicate data in the different sets of lists to be inserted ( e.g. , perhaps the second list of 1000 objects has one or two records that are identical to some of the objects previously inserted in the first set of the bulk insert). Here's the problem : when the code goes to bulk insert a set of 1000

How to have mongodb (pymongo) return _id as key value pair in response instead of object structure?

大兔子大兔子 提交于 2019-12-13 06:34:48
问题 I am using pymongo with flask. I my use case, I can have mongodb's document _id either as integer (migrated data from relational store) or the monogo's ObjectId() (generated). In get response I get something like this in each case respectively: 1st case (with integer id): { "_id": 123456, "name": "something.." } 2nd case with ObjectId() { "_id": { "$oid": "55f5efc60640fd09620c109c" }, "name": "something.." } I want to have uniform structure in both the cases - So I would like the 2nd case to