简体   繁体   中英

Using $or clause in pymongo

I have these fields in a collection:

[{
     u'_id': ObjectId('4d1f7b4d5d256b18c8000000'),
     u'name': u'1'
 },{
     u'_id': ObjectId('4d1f7b505d256b18c8000001'),
     u'name': u'2'
 },{
     u'_id': ObjectId('4d1f7b525d256b18c8000002'),
     u'name': u'3'
 }]

Then I make a query like this:

 [x for x in c.things.find( {'$or' : [{'name':'1'}, {'name':'2'}] } )]

and I expect to get:

[{
    u'_id': ObjectId('4d1f7b4d5d256b18c8000000'),
    u'name': u'1'
}, {
    u'_id': ObjectId('4d1f7b505d256b18c8000001'),
    u'name': u'2'
}]

Unfortunately not. What am I doing wrong?

I was using mongodb from the ubuntu repository which is older than 1.5.3 (The documentation of mongodb says that $or is available from 1.5.3). After I changed to the latest release (1.6.5) everything is working as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM