python - How to use `$or` in mongodb -


i new mongodb , have met issue when use $or operator.

here code:

for element in table1.find():         total += 1         result = table2.find_one({'guanzhi' : element['guanzhi'],                                    'name'    : element['name'],                                   'minzu'   : element['minzu'],                                   'keju'    : element['keju']})          if result , (element['qifen'] == result['qifen'] or                         element['qifen'] == result['qifenhuo']):             pass         else:             failed += 1 

about (element['qifen'] == result['qifen'] or element['qifen'] == result['qifenhuo']), want use $or, don't know how write code.

i changed this:

for element in table1.find({'ji': ""}):         if table2.find_one({'guanzhi' : element['guanzhi'],                             'name'    : element['name'],                             'minzu'   : element['minzu'],                             'nian'    : element['nian'],                             '$or'     : [{'qifen' : element['qifen']}, {'qifenhuo': element['qifen']}]}):             match.write(formatelement(element))         else:             no_match.write(formatelement(element))             failed += 1 

but confused it, code alright? want:

(guanzhi' : element['guanzhi']) ,  ('name' : element['name'])      ,  ('minzu'   : element['minzu'])  ,  ('keju': element['keju'])       ,  ({'qifen' : element['qifen']}   or  {'qifenhuo': element['qifen']}) 

if understand correctly, think need include both $and operator , $or operator...

what understand you're trying is: "the document you've specified" , ({'qifen' : element['qifen']} or 'qifenhuo': element['qifen']})

if so... try combining code this:

db.inventory.find( {     $and : [         // code here,         { $or : [ { sale : true }, { qty : { $lt : 20 } } ] }     ] } ) 

Comments