mongodb中的编辑对我来说似乎晦涩难懂 [英] Redact in mongodb seems obscure to me

查看:77
本文介绍了mongodb中的编辑对我来说似乎晦涩难懂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在编辑,我不确定是否了解它.

I'm fighting with redact right now and I'm not sure to understand it.

我刚刚阅读了文档,并尝试在集合等级上使用修订(它来自mongodb在线培训)

I just read the docs and tried to use redact on a collection grades (it comes from mongodb online training)

集合成绩"中的文档如下所示:

A document in the collection "grades" looks like this :

{
    "_id" : ObjectId("50b59cd75bed76f46522c34e"),
    "student_id" : 0,
    "class_id" : 2,
    "scores" : [ 
        {
            "type" : "exam",
            "score" : 57.92947112575566
        }, 
        {
            "type" : "quiz",
            "score" : 21.24542588206755
        }, 
        {
            "type" : "homework",
            "score" : 68.19567810587429
        }, 
        {
            "type" : "homework",
            "score" : 67.95019716560351
        }, 
        {
            "type" : "homework",
            "score" : 18.81037253352722
        }
    ]
}

我使用以下查询:

db.grades.aggregate([
    { $match: { student_id: 0 } },
    { 
        $redact: {
            $cond: {
                if: { $eq: [ "$type" , "exam" ] },
                then: "$$PRUNE",
                else: "$$DESCEND"
            }
        }
    }

] );

使用此查询,可以找到每种类型的考试,该子文档应排除在外.它有效,结果是:

With this query, each type an exam is found, this sub document should be excluded. And it works, the result is:

{
    "_id" : ObjectId("50b59cd75bed76f46522c34e"),
    "student_id" : 0,
    "class_id" : 2,
    "scores" : [ 
    {
        "type" : "quiz",
        "score" : 21.24542588206755
    }, 
    {
        "type" : "homework",
        "score" : 68.19567810587429
    }, 
    {
        "type" : "homework",
        "score" : 67.95019716560351
    }, 
    {
        "type" : "homework",
        "score" : 18.81037253352722
    }
]
}

但是如果我将条件取反,我希望结果中仅保留考试:

but if I invert the condition, I expect that only exams are kept in the result :

if: { $eq: [ "$type" , "exam" ] },
       then: "$$DESCEND",
       else: "$$PRUNE" 

但是结果为空.

我不明白为什么不包括考试"类型的子文档.

I don't understand why subdocument of type "exam" are not included.

推荐答案

$redact阶段从根文档及其字段开始,只有当该文档满足$$DESCEND的条件时,它才会检查子文档.包含在该文档中.这意味着$ redact对您的文档所做的第一件事就是检查以下内容:

The $redact stage starts at the root document and its fields, and only when that document fulfills the condition to $$DESCEND, it examines the sub-documents included in that document. That means the first thing $redact does with your document is examine this:

{
    "_id" : ObjectId("50b59cd75bed76f46522c34e"),
    "student_id" : 0,
    "class_id" : 2,
    "scores" : [] // Some array. I will look at this later.
}

在这里甚至都找不到type字段,因此$eq: [ "$type" , "exam" ]为false.当条件为假时,您告诉$ redact做什么? else: "$$PRUNE",因此在检查子文档之前会修剪整个文档.

It doesn't even find a type field here, so $eq: [ "$type" , "exam" ] is false. What did you tell $redact to do when the condition is false? else: "$$PRUNE", so the whole document is pruned before the sub-documents are examined.

作为一种解决方法,请测试$type是否为"exam"或不存在.您没有明确要求一个可行的解决方案,因此,我将作为练习来帮助您解决该问题.

As a workaround, test if $type is either "exam" or doesn't exist. You didn't explicitly ask for a working solution, so I will leave it as an exercise to you to figure out how to do this.

这篇关于mongodb中的编辑对我来说似乎晦涩难懂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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