Find documents which contain a particular value - Mongo, Python
<p>I'm trying to add a search option to my website but it doesn't work. I looked up solutions but they all refer to using an actual string, whereas in my case I'm using a variable, and I can't make those solutions work. Here is my code:</p>

<pre><code>cursor = source.find({'title': search_term}).limit(25)
for document in cursor:
    result_list.append(document)
</code></pre>

<p>Unfortunately this only gives back results which match the search_term variable's value exactly. I want it to give back any results where the title contains the search term - regardless what other strings it contains. How can I do it if I want to pass a variable to it, and not an actual string? Thanks.</p>

<p>You can use <code>$regex</code> to do contains searches.</p>

<pre><code>cursor = collection.find({'field': {'$regex':'regular expression'}})
</code></pre>

<p>And to make it case insensitive:</p>

<pre><code>cursor = collection.find({'field': {'$regex':'regular expression', '$options'ââ:'i'}})
</code></pre>

<p>Please try <code>cursor = source.find({'title': {'$regex':search_term}}).limit(25)</code></p>

python mongodb