pymongo

pymongo group by datetime

余生长醉 提交于 2019-12-19 08:53:34
问题 Im trying to search through a collection and group records by date field which is a datetime. I know pymongo converts those to the proper type on the background (ISODate or something like that). Question is, since datetime objects have date, time, timezone.. how can i tell the group operator to use only the date portion? Because otherwise i dont get the desired grouping since time is preventing the records with same day, month, year to be grouped together. db.test.aggregate([ {"$group": { "

How to read through collection in chunks by 1000?

房东的猫 提交于 2019-12-19 05:51:12
问题 I need to read whole collection from MongoDB ( collection name is "test" ) in Python code. I tried like self.__connection__ = Connection('localhost',27017) dbh = self.__connection__['test_db'] collection = dbh['test'] How to read through collection in chunks by 1000 ( to avoid memory overflow because collection can be very large ) ? 回答1: I agree with Remon, but you mention batches of 1000, which his answer doesn't really cover. You can set a batch size on the cursor: cursor.batch_size(1000);

Pip install error. Setuptools.command not found

十年热恋 提交于 2019-12-19 05:11:21
问题 I'm using a clean instance of Ubuntu server and would like to install some python packages in my virtualenv. I receive the following output from the command 'pip install -r requirements.txt' Downloading/unpacking pymongo==2.5.2 (from -r requirements.txt (line 7)) Downloading pymongo-2.5.2.tar.gz (303kB): 303kB downloaded Running setup.py egg_info for package pymongo Traceback (most recent call last): File "<string>", line 3, in <module> ImportError: No module named setuptools.command Complete

MongoDB: Removing a field from ALL subdocuments in an array field

拟墨画扇 提交于 2019-12-18 16:56:25
问题 I have thousands of documents in this format: { "_id" : ObjectId("51e98d196b01c2085c72d731"), "messages" : [ { "_id" : ObjectId("520167056b01c20bb9eee987"), "id" : ObjectId("520167056b01c20bb9eee987"), }, { "_id" : ObjectId("520167056b01c20bb9eee988"), "id" : ObjectId("520167056b01c20bb9eee988"), }, { "_id" : ObjectId("520167056b01c20bb9eee989"), "id" : ObjectId("520167056b01c20bb9eee989"), } ], } I need to remove the duplicate "id" field. This is what I have tried: db.forum_threads.update({}

Migrating to MongoDB: how to query GROUP BY + WHERE

早过忘川 提交于 2019-12-18 15:54:06
问题 I have a MYSQL table with records of people's names and the time of arrival expresed as a number. Think of it as a marathon. I want to know how many people arrived into a certain gap of time who where named the same, so: SELECT name, COUNT(*) FROM mydb.mytable WHERE Time>=100 AND Time<=1000 GROUP BY name And as results I get: Susan, 1 John, 4 Frederick, 1 Paul, 2 I'm migrating to MongoDB now, and using Python to code (so I'm asking for Pymongo help). I tried looking for information about the

Get a list of all unique tags in mongodb

时光毁灭记忆、已成空白 提交于 2019-12-18 11:52:26
问题 I am beginning with mongodb and have a collection with documents that look like the following { "type": 1, "tags": ["tag1", "tag2", "tag3"] } { "type": 2, "tags": ["tag2", "tag3"] } { "type": 3, "tags": ["tag1", "tag3"] } { "type": 1, "tags": ["tag1", "tag4"] } With this, I want a set of all the tags for a particular type. For example, for type 1, I want the set of tag1, tag2, tag3, tag4 (any order). All I could think of is to get the tags and add them to a set in python, but I wanted to know

Is it possible to use Variable for collection name using pymongo?

爱⌒轻易说出口 提交于 2019-12-18 11:45:36
问题 Is it possible to use Variable for collection name using pymongo? for example: col = 'my_collection' db.col.update() 回答1: You can use: col = 'my_collection' db[col].update() reference 回答2: You're trying to call a method from a string. This is not specific to pymongo. You can use getattr to see if the string exists as an attribute on your db object, then call it. e.g. my_collection = getattr(col, 'my_collection') my_collection.update() edit: Note that using the getattr approach allows for

How to find names of all collections using PyMongo?

核能气质少年 提交于 2019-12-18 10:58:30
问题 How to find names of all collections using PyMongo and find all fields in chosen collection ? I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list, when user click on one item need to find all fields in that collection) 回答1: To find the collections, you can use collection_names() - http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names 回答2: This

Self-signed SSL connection using PyMongo

断了今生、忘了曾经 提交于 2019-12-18 10:56:58
问题 I'm trying to create a secure SSL connection to MongoDB using PyMongo. The goal is to use this configuration for a Mongo instance running on EC2 to which I can connect with a Python client. For testing, I'm just trying to get the configuration working locally first. My as yet failing attempt can be found here. Short version of what I think is the problem: My client side certificate authority file ca.pem isn't correct. The way I have it, this file is actually identical to the one I'm using

Abstract classes and PyMongo; can't instantiate abstract class

☆樱花仙子☆ 提交于 2019-12-18 09:44:55
问题 I created the empty abstract class AbstractStorage and inherited the Storage class from it: import abc import pymongo as mongo host = mongo.MongoClient() print(host.alive()) # True class AbstractStorage(metaclass=abc.ABCMeta): pass class Storage(AbstractStorage): dbh = host def __init__(self): print('__init__') Storage() I expected the output to be True __init__ however, the one I'm getting is True Traceback (most recent call last): File "/home/vaultah/run.py", line 16, in <module> Storage()