MongoEngine 查询
数据库查询
Document 对象有一个 objects 属性,用来访问在数据库中跟这个类有关的对象。这个 objects 属性其实是一个 QuerySetManager ,它会创建和返回一个新的 QuerySet 对象的访问。这个 QuerySet 对象可以从数据库中遍历获取的文档:
1 | # Prints out the names of all the users in the database |
过滤查询
可以通过调用 QuerySet 对象的关键字参数来对数据查询进行过滤,关键字查询中的键和你想要查询的 Document 中的字段一致:
1 | # This will return a QuerySet that will only iterate over users whose |
对于内嵌 document 的字段可以使用 __ 来代替对象属性访问语法中的 . 进行访问:
1 | # This will return a QuerySet that will only iterate over pages that have |