Question

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} } 
       )

act_obs = self.db.action_log.find(
        {'dest_id': id1, 'src_id': {'$in': ids} } 
       )

However, and this is where I can't figure out what is wrong, the following refuses to return any results at all:

act_obs = self.db.action_log.find(
        {'$or': [
          {'dest_id': id1, 'src_id': {'$in': ids} },
          {'src_id': id1, 'dest_id': {'$in': ids} }
        ]}
       )

Can someone shed some light on what I'm doing wrong, if that is the case? And more importantly, how to accomplish what I'm trying to do within mongo.

I'm trying to get all the records where id1 is the src_id and any of the ids in the ids list is the dest_id OR any records where id1 is the dest_id and any of the ids in the ids list is the src_id.

I'm using pymongo 1.7. Thank you!

Was it helpful?

Solution

The $or operator is available in MongoDB 1.5.3 and later.

An alternative is to use a Javascript function, something like...

find = self.db.action_log.find()
find.where(pymongo.code.Code('this.dest_id==1 || this.src_id==2'))

OTHER TIPS

I don't think that you can use $or like that. You will have to perform the union client side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top