Pymongo $in Query Not Working

旧城冷巷雨未停 提交于 2019-12-06 21:22:34

问题


Seeing some strange behavior in Pymongo $in query. Looking for records that meet the following query:

speciesCollection.find({"SPCOMNAME":{"$in":['paddlefish','lake sturgeon']}})

The query returns no records.

If I change it to find_one the it works returning the last value for Lake Sturgeon. The field is a text filed with one vaule. So I am looking for records that match paddlefish or Lake Sturgeon.

It works fine in Mongo Shell like this:

speciesCollection.find({SPCOMNAME:{$in: ['paddlefish','lake strugeon']}},{_id:0})

Here is the result from shell

{ "SPECIES_ID" : 1, "SPECIES_AB" : "LKS", "SPCOMNAME" : "lake sturgeon", "SP_SCINAME" : "Acipenser fulvescens
{ "SPECIES_ID" : 101, "SPECIES_AB" : "PAH", "SPCOMNAME" : "paddlefish", "SP_SCINAME" : "Polyodon spathula" }

Am I missing something here?


回答1:


I think you have a typo or some other error in your program as I just did a test with your sample data and query and it works - see the GIF

Below is my test code which connects to the database called so and the collection speciesCollection, maybe you find the error in yours with it

import pymongo

client = pymongo.MongoClient('dockerhostlinux1', 30000)
db = client.so
coll = db.speciesCollection

result = coll.find({"SPCOMNAME":{"$in":['paddlefish','lake sturgeon']}})
for doc in result:
    print(doc)



来源:https://stackoverflow.com/questions/38919756/pymongo-in-query-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!