Trying to create a MongoDB query with the Pymongo driver that search a database for some dog breeds, but I need to do pattern matching due to data being of differing quality. I know I cannot use Regex in a $in style query so I am using JS patterns.
I have this so far:
df = shelter.getRecords({
"breed": {"$in": [/labrador/i, /chesa/i, /newfound/i]}
})
But I get syntax errors. Command seems to work in Mongo shell....is this a limitation of Pymongo?
You could use "$regex"
like this.
N.B.: Your pymongo
query filter could be a python
dictionary like {'breed': {'$regex': '(labrador)|(chesa)|(newfound)', '$options': 'i'}}
.
db.shelter.find({
"breed": {
"$regex": "(labrador)|(chesa)|(newfound)",
"$options": "i"
}
})
Example output:
[
{
"_id": 21,
"breed": "Chesapeake Retriever"
},
{
"_id": 80,
"breed": "Newfoundland"
}
]
Try it on mongoplayground.net.