【问题标题】:Sophisticated string queries in MongoAlchemyMongoAlchemy 中复杂的字符串查询
【发布时间】:2012-05-19 13:46:24
【问题描述】:

我正在学习 MongoAlchemy,它是 MongoDB 的 Python 驱动程序之上的一个层。假设有一个 Python 类,映射自 MongoDB 对象,如下所示:

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

我这样查询数据库:

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

它工作正常,但现在我想在数据库中查询不区分大小写的名称(“geronimo”和“Geronimo”),或者查找其中包含两个 o 的所有名称。 query.filter(Person.name[1:] == 'eronimo') 等不起作用,因为 Person.name 不是字符串,而是 QueryField 对象。

如何在 MongoAlchemy 中进行如此复杂的查询?

【问题讨论】:

    标签: python mongodb pymongo mongoalchemy


    【解决方案1】:

    要查找任何共现,您可以在查询中使用运算符。像这样:

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

    $regex 运算符使用正则表达式,允许我们在文本字段中进行更复杂的搜索。

    这是 mongo 查询运算符的链接。

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

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2010-10-06
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      相关资源
      最近更新 更多