简体   繁体   English

部分文本搜索 Pymongo

[英]Partial Text Search Pymongo

I want to discover all the persons named John that are in the cast of the collection show_info.我想发现集合 show_info 中所有名为 John 的人。 This works in the mongo shell.这适用于 mongo shell。 db.show_info.find({ 'cast': /john/i})

But when I do this in python it won't work because I have to convert /john/i to a string.但是当我在 python 中执行此操作时,它不起作用,因为我必须将 /john/i 转换为字符串。

This does not work in python using pymongo:这在使用 pymongo 的 python 中不起作用:

quote = {'cast': "/" + name + "/i"}
results = db.show_info.find(quote)

How can I make this query work in python using pymongo?如何使用 pymongo 使这个查询在 python 中工作?

You can use $regex , see more in the doc您可以使用$regex ,在文档中查看更多信息

{
  "cast": {
    "$regex": "john",
    "$options": "i"
  }
}

try it here在这里试试

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

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