pymongo

MongoDB/py-mongo for queries with date functions

Im looking to use a document database such as MongoDB but looking through the documents I cant find much on queries that involve date functions. For example lets say that I'm asking one of the following questions of the DB: "Tell me all the people who bought a product on tuesday" "Get me all sales and group by month" They are random ...

MongoDB query to return only embedded document

assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i query for and have MongoDB return only Comment objects matching my query spec? eg, db.blog_posts.find({"comment.submitter": "some_name"}) returns only a list of comments. edit: an example: import pymongo connection = pymongo.Connection() db = conn...

MongoDB: Limiting results from a $gt query (from pymongo)

Hi there, I'm gathering some statistics from a web service, and storing it in a collection. The data looks similar to this (but with more fields): {"downloads": 30, "dt": "2010-02-17T16:56:34.163000"} {"downloads": 30, "dt": "2010-02-17T17:56:34.163000"} {"downloads": 30, "dt": "2010-02-17T18:56:34.163000"} {"downloads": 30, "dt": "201...

TG2.1: Proper location to store a database session instance?

I am using a custom database (MongoDB) with TG 2.1 and i am wondering where the proper place to store the PyMongo connection/database instances would be? Eg, at the moment they are getting created inside of my inherited instance of AppConfig. Is there a standard location to store this? Would shoving the variables into the project.model....

pymongo: problem with findandmodify - "no such command" is returned

I believe there is a bug in pymongo (or, at least, the documentation) which makes it impossible to run a findandupdate query. Here's what happens. When I run: result = db.command({ 'findandmodify': 'my_collection', 'query': {'foo': 'bar'}, 'update': {'$set': {'status': 'queued'}}, }) The query that act...

Several operations inside of a single MongoDB query

Is it possible to alter more than one collection in a single query with MongoDB? A SQL example: begin update table_1 set some_field=1; update table_2 set a_different_field=2; commit ...

Filtering documents against a dictionary key in MongoDB

I have a collection of articles in MongoDB that has the following structure: { 'category': 'Legislature', 'updated': datetime.datetime(2010, 3, 19, 15, 32, 22, 107000), 'byline': None, 'tags': { 'party': ['Peter Hoekstra', 'Virg Bernero', 'Alma Smith', 'Mike Bouchard', 'Tom George', 'Rick Snyder'], 'g...

python mongokit Connection() AssertionError

just installed mongokit and can't figure out why I get AssertionError python console: >>> from mongokit import Connection >>> c = Connection() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.6/dist-packages/mongokit-0.5.3-py2.6.egg/mongokit/connection.py", line 35, in __init__ ...

Using a '.' in your key name in MongoDB (PyMongo)

When I try saving a dict with a '.' in the key PyMongo throws an error (InvaildName) however I do see (on the Mongodb website) that keys can have '.''s in them. Why is it that pymongo won't let me save these docs? Is there an issue with them and Mongo? James ...

Mongo not returning full data from query

I am using PyMongo and Mongo version db version v1.4.1, pdfile version 4.5 When I try to query the database I am always getting different results. The code looks like this: familycollection = conn.picdata pics = familycollection[place] pictures = [i for i in pics.find()] return pictures However pictures doesn't full return all my dat...

How to query through a DBRef in MongoDB/pymongo?

Is it possible to query through a DBRef using a single find spec? user collection { 'age': 30 } post collection { 'user': DBRef('user', ...) } Is it possible to query for all post who's users are 30 in a single find step? If not, would it be wise to create a javascript function to handle the multi-stage operation or will ...

Can DBRefs contain additional fields?

I've encountered several situations when using MongoDB that require the use of DBRefs. However, I'd also like to cache some fields from the referenced document in the DBRef itself. {$ref:'user', $id:'10285102912A', username:'Soviut'} For example, I may want to have the username available even though the user document is referenced. ...

Mongodb: Connection reset by peer

I have a Mongo server running on an Ubuntu box, and I am trying to connect to it with pymongo using the usual syntax: from pymongo import Connection c = Connection('db.example.com', 27017) This works just fine on a recent-model Intel mac (OS 10.6). However, the same code on an older G5 tower (10.5) throws this error: pymongo.errors....

How to manually create a DBRef using pymongo?

I want to create a DBRef manually so that I can add an additional field to it. However, when I try to pass the following: {'$ref': 'projects', '$id': '1029412409721', 'project_name': 'My Project'} Pymongo raises an error: pymongo.errors.InvalidName: key '$id' must not start with '$' It would seem that pymongo reserve the $ for the...

How do I get the ID of an object after persisting it in PyMongo?

I have a PyMongo newbie question: If collection is a PyMongo Collection and I use it to save an object with obj = {'foo': 'bar'} collection.insert(obj) then MongoDB automatically generates an _id field for obj; once can confirm this with print obj which yields something like {'foo': 'bar', '_id': ObjectId('4c2fea1d289c7d837e000000...

Multiple simultaneous updates with MongoDB/PyMongo?

According to the PyMongo docs, update() can only update a single document at a time. Let's say I have 100 documents I want to update simultaneously. That's a lot of overhead. Is there a way to update multiple documents with a single MongoDB query through PyMongo? ...

How to get sorted results in MongoKit?

How can I perform query like: db.articles.find().sort({created_at: -1}).limit(5); in MongoKit driver? Perhaps I'm blind, but I can't find it in manual. I want to retrieve 5 last products ordered by 'created_at'. MongoKit offers to sort result list (it's more then 2000 items) and slice it. It's unaccepable, I want to sort() and limit() o...

Creating indexes - MongoDB

Hi folks, my "table" look like this: {'name':'Rupert', 'type':'Unicorn', 'actions':[ {'time':0, 'position':[0,0], 'action':'run'}, {'time':50, 'position':[50,0], 'action':'stoprun'}, {'time':50, 'position':[50,0], 'action':'jump'}, {'time':55, 'position':[50,0], 'action':'laugh'}, ... ]} any way I can index the it...

Various queries - MongoDB

Hi folks, This is my table: unicorns = {'name':'George', 'actions':[{'action':'jump', 'time':123123}, {'action':'run', 'time':345345}, ...]} How can I perform the following queries? Grab the time of all actions of all unicorns where action=='jump' ? Grab all actions of...

mongodb union $or problems

I have a collection which is an action log between two users. It has a src_id and a dest_id. I'm a looking to fetch all the records that are actions between id1 and a list of ids - "ids = [id2, id3, id4]". The following two statements work properly: act_obs = self.db.action_log.find( {'src_id': id1, 'dest_id': {'$in': ids} } ...