简体   繁体   English

Pymongo $在查询中不起作用

[英]Pymongo $in Query Not Working

Seeing some strange behavior in Pymongo $in query. $in查询中看到Pymongo $in一些奇怪行为。 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. 如果我将它更改为find_one,它将返回Lake Sturgeon的最后一个值。 The field is a text filed with one vaule. 该字段是一个带有一个vaule的文本。 So I am looking for records that match paddlefish or Lake Sturgeon. 所以我正在寻找与白鲟或鲟鱼湖相匹配的记录。

It works fine in Mongo Shell like this: 它在Mongo Shell中运行良好,如下所示:

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

Here is the result from shell 这是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? 我在这里错过了什么吗?

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 我认为您的程序中存在拼写错误或其他错误,因为我只是对您的示例数据和查询进行了测试并且它有效 - 请参阅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 下面是我的测试代码连接到名为so的数据库和集合speciesCollection ,也许你用它找到了你的错误

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)

GIF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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