1

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?

van
  • 74,297
  • 13
  • 168
  • 171
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166

1 Answers1

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158