本文介绍了如何在mongoengine中使用python在字典中包含的嵌套字典或数组上执行此类过滤器查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下字典对象:

{
    "a": "value of a",
    "somedict": {
        "someinfo": [
            {
                "name": "Jordan",
                "food": [
                    "fries",
                    "coke",
                    "drink"
                ]
            }
        ]
    }
}

如果我想使用mongoengine在python中应用查询过滤器,该怎么办?我在文档中看到您可以执行以下操作:

If I wanted to apply a query filter in python using mongoengine, how would I do it? I see in the documentation you can do things like:

sample_objs_filter = Sample.objects(a='value of a')

但是我该如何过滤说

1) "name='Jordan'" 
2)'food' contains 'fries'?

如果mongoengine无法做到,那么还有其他更好的Mongo库吗?

If mongoengine cant do it, is there some other mongo library thats better at accomplishing this?

推荐答案

我建议阅读更多有关mongodb的点符号,说明如何查询/查看对象并返回匹配的文档.

I'd suggest reading more about mongodb's dot notation about how you can query / look into objects and return matching documents.

因为您不能使用点作为关键字参数,所以 mongoengine 遵循django orm风格的double下划线:

As you can't use a dot as a keyword argument mongoengine follows the django orm style of double underscores:

1) Sample.objects(somedict__someinfo__name='Jordan')
2) Sample.objects(somedict__someinfo__food='Fries')

这篇关于如何在mongoengine中使用python在字典中包含的嵌套字典或数组上执行此类过滤器查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 01:54