Pymongo $ in查询不起作用 [英] Pymongo $in Query Not Working

查看:269
本文介绍了Pymongo $ in查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Pymongo $in查询中看到一些奇怪的行为.寻找符合以下查询的记录:

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

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

查询不返回任何记录.

如果我将其更改为find_one,它将返回works鱼湖的最后一个值.该字段是一个带有一个小节的文本字段.因此,我正在寻找与pad鱼或Lake鱼湖相匹配的记录.

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.

在Mongo Shell中可以这样正常工作:

It works fine in Mongo Shell like this:

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

这是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" }

我在这里想念东西吗?

推荐答案

我认为您的程序中有错别字或其他错误,因为我刚刚对示例数据和查询进行了测试并且可以正常工作-请参阅GIF

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

下面是我的测试代码,该代码连接到名为so和集合speciesCollection的数据库,也许您会在其中发现错误

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)

这篇关于Pymongo $ in查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆