Mongo嵌入式文档查询 [英] Mongo Embedded Document Query

查看:61
本文介绍了Mongo嵌入式文档查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个DynamicDocuments:

I've 2 DynamicDocuments:

class Tasks(db.DynamicDocument):
    task_id = db.UUIDField(primary_key=True,default=uuid.uuid4)
    name = db.StringField()
    flag = db.IntField()

class UserTasks(db.DynamicDocument):
    user_id = db.ReferenceField('User')
    tasks = db.ListField(db.ReferenceField('Tasks'),default=list)

我想通过检查给定task_id和user_id的给定task_id的flag值(来自Tasks Document)是0还是1来过滤UserTasks文档.所以我以以下方式查询:-

I want to filter the UserTasks document by checking whether the flag value (from Tasks Document) of the given task_id is 0 or 1, given the task_id and user_id. So I query in the following way:-

obj = UserTasks.objects.get(user_id=user_id,tasks=task_id)

这为我带来了一个UserTask对象.

This fetches me an UserTask object.

现在,我循环浏览任务列表,首先获得等效任务,然后按以下方式检查其标志值.

Now I loop around the task list and first I get the equivalent task and then check its flag value in the following manner.

task_list = obj.tasks
for t in task_list:
    if t['task_id'] == task_id:
        print t['flag']

是否有任何更好/直接的方法来查询UserTasks文档以获取任务文档的标志值.

Is there any better/direct way of querying UserTasks Document in order to fetch the flag value of Tasks Document.

PS:我本可以直接从Tasks文档中获取标志值,但是我还需要检查任务是否与用户相关联.因此,我直接查询了USerTasks文档.

PS : I could have directly fetched flag value from the Tasks Document, but I also need to check whether the task is associated with the user or not. Hence I directly queried the USerTasks document.

推荐答案

我们可以直接使用 ,无法直接使用ReferenceField字段过滤文档,因为这样做将需要联接,而mongodb不支持联接.

No, its not possible to directly filter a document with the fields of ReferenceField as doing this would require joins and mongodb does not support joins.

根据数据库参考上的MongoDB文档:

MongoDB不支持联接..在MongoDB中,某些数据被非规范化, 或与相关数据一起存储在文档中,以消除加入联接的需要.

MongoDB does not support joins. In MongoDB some data is denormalized, or stored with related data in documents to remove the need for joins.

从另一个页面官方网站:

如果我们使用关系数据库,则可以在 用户和商店,并在单个查询中获取所有对象. 但是 MongoDB不支持联接,因此有时需要一些 非规范化.

If we were using a relational database, we could perform a join on users and stores, and get all our objects in a single query. But MongoDB does not support joins and so, at times, requires bit of denormalization.

关系纯粹主义者可能已经感到不安,好像我们在 违反了一些普遍的法律.但是请记住,MongoDB 集合不等同于关系表;每个服务一个 独特的设计目标.规范化的表格提供了一个原子的, 孤立的数据块.但是,文档更能代表 整体上的一个对象.

Relational purists may be feeling uneasy already, as if we were violating some universal law. But let’s bear in mind that MongoDB collections are not equivalent to relational tables; each serves a unique design objective. A normalized table provides an atomic, isolated chunk of data. A document, however, more closely represents an object as a whole.

因此,在1个查询中,我们不能同时过滤具有特定标志值和UserTasks模型上给定的user_idtask_idtasks.

So in 1 query, we can't both filter tasks with a particular flag value and with the given user_id and task_id on the UserTasks model.

然后如何执行过滤?

要根据所需条件执行过滤,我们将需要执行2个查询.

To perform the filtering as per the required conditions, we will need to perform 2 queries.

在第一个查询中,我们将尝试使用给定的task_idflag过滤Tasks模型.然后,在第二个查询中,我们将使用给定的user_id和从第一个查询中检索到的task过滤UserTasks模型.

In the first query we will try to filter the Tasks model with the given task_id and flag. Then, in the 2nd query, we will filter UserTasks model with the given user_id and the task retrieved from the first query.

示例:

让我们说我们有一个user_idtask_id,我们需要检查相关任务是否具有flag值作为0.

Lets say we have a user_id, task_id and we need to check if the related task has flag value as 0.

第一个查询

我们将首先使用给定的task_idflag作为0检索my_task.

We will first retrive the my_task with the given task_id and flag as 0.

my_task = Tasks.objects.get(task_id=task_id, flag=0) # 1st query

第二个查询

然后在第二个查询中,您需要使用给定的user_idmy_task对象对UserTask模型进行过滤.

Then in the 2nd query, you need to filter on UserTask model with the given user_id and my_task object.

my_user_task = UserTasks.objects.get(user_id=user_id, tasks=my_task) # 2nd query

仅当获得具有给定task_idflag值的my_task对象时,才应执行第二次查询.另外,如果没有匹配的对象,您将需要添加错误处理.

You should perform 2nd query only if you get a my_task object with the given task_id and flag value. Also, you will need to add error handling in case there are no matched objects.

如果我们将EmbeddedDocument用于Tasks模型怎么办?

What if we have used EmbeddedDocument for the Tasks model?

假设我们已经将Tasks文档定义为EmbeddedDocument,并将UserTasks模型中的tasks字段定义为EmbeddedDocumentField,那么要进行所需的过滤,我们可以执行以下操作: /p>

Lets say we have defined our Tasks document as an EmbeddedDocument and the tasks field in UserTasks model as an EmbeddedDocumentField, then to do the desired filtering we could have done something like below:

my_user_task = UserTasks.objects.get(user_id=user_id, tasks__task_id=task_id, tasks__flag=0)

从任务列表中获取特定的my_task

Getting the particular my_task from the list of tasks

上面的查询将返回一个UserTask文档,其中将包含所有tasks.然后,我们需要执行某种迭代才能获得所需的任务.

The above query will return a UserTask document which will contain all the tasks. We will then need to perform some sort of iteration to get the desired task.

为此,我们可以使用enumerate()进行列表理解. 然后,所需的索引将成为返回的1元素列表的第一个元素.

For doing that, we can perform list comprehension using enumerate(). Then the desired index will be the 1st element of the 1-element list returned.

my_task_index = [i for i,v in enumerate(my_user_task.tasks) if v.flag==0][0]

这篇关于Mongo嵌入式文档查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆