使用$ redact运算符在MongoDB 2.6中进行字段级访问

时间:2014-04-14 22:45:32

标签: json mongodb security

我有一个带有嵌套子部分的文档结构,我只想检索与传入的Tag匹配的子部分。下面是json结构。

{
        "_id" : 1,
        "title" : "Document Formatting",
        "creationDate" : 2014,
        "tags" : [ 
            "GLTW", 
            "SP", 
            "TK"
        ],
        "subsections" : [ 
            {
                "subtitle" : "Section 1: Overview",
                "tags" : [ 
                    "SI", 
                    "GLTW"
                ],
                "content" : "Section 1: This is the content of section 1.",
                "subsections" : [ 
                    {
                        "subtitle" : "Section 1.1: Analysis",
                        "tags" : [ 
                            "SP"
                        ],
                        "content" : "Section 1.1: This is the content of section 1.1.",
                        "subsections" : [ 
                            {
                                "subtitle" : "Section 1.1.1: Analysis",
                                "tags" : [ 
                                    "TK"
                                ],
                                "content" : "Section 1.1.1: This is the content of section 1.1.1."
                            }
                        ]
                    }
                ]
            }
        ] }

以下是我尝试的查询,但无效:

var userAccess = ["TK"]; 
db.redact.aggregate(    [
        { $match: { creationDate: 2014 } },
        { $redact:
            {
               $cond:
                  {
                    if: { $gt: [ { $size: { $setIntersection: [  "$tags",userAccess ] } }, 0 ] },
                    then: "$$DESCEND",
                    else: "$$PRUNE"
                  }
            }
        }    ] )

以上查询不返回任何内容。

但是,如果我将userAccess更改为var userAccess = ["TK","SP","GLTW"];,则表明它正常工作。在这种情况下,如何只得到与userAccess匹配的子部分userAccess = [“TK”]。任何帮助将不胜感激。

0 个答案:

没有答案