Search code examples
pythonmongodbpymongo

Partial Text Search Pymongo


I want to discover all the persons named John that are in the cast of the collection show_info. This works in the 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.

This does not work in python using pymongo:

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

How can I make this query work in python using pymongo?


Solution

  • You can use $regex, see more in the doc

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

    try it here