如何在子属性上使用$ setIsSubset(或其他set操作)

时间:2014-06-27 02:12:43

标签: mongodb aggregation-framework

我有一个我认为是一个非常简单的问题,我只是错过了一些东西所以我希望有人可以指出我哪里出错了。我正在尝试使用$ setIsSubset操作引用包含项列表的子属性,但它告诉我它是null。基本上查询是错误的,但我不明白为什么。这是Mongo shell的输出:

$ mongo setTest
MongoDB shell version: 2.6.3
connecting to: setTest
> db.items.insert({"credentials":{"authorities":["AUTH1"]}});
WriteResult({ "nInserted" : 1 })
> db.items.find();
{ "_id" : ObjectId("53acd0e214c4ee1272550de4"), "credentials" : { "authorities" : [ "AUTH1" ] } }
> var userAuthorities = ["AUTH1","AUTH2","AUTH3"];
> db.items.aggregate([
...     { $redact:
...         {
...            $cond:
...               {
...                  if: { $setIsSubset: ["$credentials.authorities", userAuthorities ] },
...                  then: "$$DESCEND",
...                  else: "$$PRUNE"
...               }
...         }
...      }
... ]);
assert: command failed: {
    "errmsg" : "exception: both operands of $setIsSubset must be arrays. First argument is of type: EOO",
    "code" : 17310,
    "ok" : 0
} : aggregate failed
Error: command failed: {
    "errmsg" : "exception: both operands of $setIsSubset must be arrays. First argument is of type: EOO",
    "code" : 17310,
    "ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
    at (shell):1:10
2014-06-26T20:04:54.465-0600 Error: command failed: {
    "errmsg" : "exception: both operands of $setIsSubset must be arrays. First argument is of type: EOO",
    "code" : 17310,
    "ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13
>

谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:4)

此处的问题不在于set operators本身,而在于您对$redact的理解。

$redact管道阶段以递归方式遍历文档以测试字段值,因此这里的问题是引用数组匹配的方式实际上并不存在于当前深度&#34 ;所有的时间。

你有两种方法可以解决这个问题。首先使用$project进行操作以确保所有深度都存在数组:

var userAuthorities = ["AUTH1","AUTH2","AUTH3"];

db.items.aggregate([
    { "$project": {
        "credentials": 1,
        "authorities": { "$literal": [] }
    }},
    { "$redact": {
        "$cond": {
            "if": { "$setIsSubset": [ "$authorities", userAuthorities ] },
            "then": "$$DESCEND",
            "else": "$$PRUNE"
        }                    
    }}
])

或测试字段的存在,并通过$ifNull内联替换为空数组:

db.items.aggregate([
    { "$redact": {
        "$cond": {
            "if": { 
                "$setIsSubset": [ 
                    { "$ifNull": [ "$authorities", { "$literal": [] } ]},
                    userAuthorities
                ]
            },
            "then": "$$DESCEND",
            "else": "$$PRUNE"
        }                    
    }}
])

或者最后总是接受您正在使用$$ROOT比较。但有点挫败了$redact

的目的
db.items.aggregate([
    { "$redact": {
        "$cond": {
            "if": { 
                "$setIsSubset": [ 
                    "$$ROOT.credentials.authories"
                    userAuthorities
                ]
            },
            "then": "$$DESCEND",
            "else": "$$PRUNE"
        }                    
    }}
])

但基本上把它,与$redact的比较是相对于当前下降到的文档的级别。所以你的变量比较也必须考虑到这一点。