Mirzhan Irkegulov
Mirzhan Irkegulov

Reputation: 18055

Sophisticated string queries in MongoAlchemy

I'm learning MongoAlchemy, a layer on top of Python driver for MongoDB. Let's say there is a Python class, mapped from a MongoDB object like this:

from mongoalchemy.document import Document
from mongoalchemy.fields import *
class Person(Document):
    name = StringField()

And i query the database like this:

for person in query.filter(Person.name == 'Geronimo'):
    print person

It works fine, but now then i want to query the database for case insensitive name (both "geronimo" and "Geronimo"), or to find all names having two o's in them. query.filter(Person.name[1:] == 'eronimo') and such do not work, as Person.name is not a string, but a QueryField object.

How do i do such complex queries in MongoAlchemy?

Upvotes: 1

Views: 428

Answers (1)

To find any co-occurrence you can use operators within the query. Like this:

datosquery = datos.query.filter({datos.schema_name: {"$regex": texto}})

The $regex operator, which uses regular expressions, allows us to make more complex searches in text fields.

Here's a link to the operators for mongo queries.

https://charlascylon.com/2013-07-10-tutorial-mongodb-operaciones-de-consulta-avanzadas

Upvotes: 1

Related Questions