依赖于同一文档的Mongodb查询

依赖于同一文档的Mongodb查询,mongodb,mongodb-query,Mongodb,Mongodb Query,因此,我试图在MongoDB中编写一个查询,该查询依赖于被查询文档的一部分来返回我想要的文档 这是这些文件格式的一个示例: { "_id" : ObjectId("58990510cdab041b39c78dd1"), "classcode" : "CS433", "department" : "CS", "instructor" : { "name" : "Mike", "office" : "Starbucks"

因此,我试图在MongoDB中编写一个查询,该查询依赖于被查询文档的一部分来返回我想要的文档

这是这些文件格式的一个示例:

{
    "_id" : ObjectId("58990510cdab041b39c78dd1"),
    "classcode" : "CS433",
    "department" : "CS",
    "instructor" : {
            "name" : "Mike",
            "office" : "Starbucks"
    },
    "students" : [
            {
                    "name" : "Dave",
                    "major" : "CS",
                    "gradyear" : 2019
            },
            {
                    "name" : "Joe",
                    "major" : "CS",
                    "gradyear" : 2018
            },
            {
                    "name" : "Stan",
                    "major" : "CS",
                    "gradyear" : 2017
            }
    ]
}
我想使用表示部门的字符串来帮助查看是否存在与该部门在major中不匹配的字符串。i、 e.如果系是CS,则检查是否有学生没有CS专业


我知道
$ne
$elemMatch
,还有什么不知道的。我只是无法使用文档的另一部分来帮助查询。我不认为子查询在这里有用。

您可以使用
$redact

$redact
一次遍历一个文档级别,递归查找
major
字段,并从
$$ROOT
级别对标准执行
$$down
$$PRUNE

查询将保留所有没有
专业
匹配
的学生

db.collection.aggregate([{
    "$redact": {
        "$cond": [{
                $ne: ["$major", "$$ROOT.department"]
            },
            "$$DESCEND",
            "$$PRUNE"
        ]
    }
}])
db.collection.aggregate([
    { $redact: {
        $cond: {
            if: { "$ifNull" : ["$department", false] },
            then: { $cond: {
                if: { $anyElementTrue: {
                    $map: {
                        input: "$students",
                        as: "student",
                        in: { $ne: [ "$$student.major", "$$ROOT.department" ] }
                    }
                }},
                then: "$$DESCEND",
                else: "$$PRUNE"
            }},
            else: "$$DESCEND"
        }
    }}
])
更新:

当至少有一名学生的
专业
不匹配时,此查询将返回所有文档

db.collection.aggregate([{
    "$redact": {
        "$cond": [{
                $ne: ["$major", "$$ROOT.department"]
            },
            "$$DESCEND",
            "$$PRUNE"
        ]
    }
}])
db.collection.aggregate([
    { $redact: {
        $cond: {
            if: { "$ifNull" : ["$department", false] },
            then: { $cond: {
                if: { $anyElementTrue: {
                    $map: {
                        input: "$students",
                        as: "student",
                        in: { $ne: [ "$$student.major", "$$ROOT.department" ] }
                    }
                }},
                then: "$$DESCEND",
                else: "$$PRUNE"
            }},
            else: "$$DESCEND"
        }
    }}
])

这只返回整个文档,但不返回数组中各个学生的文档。我希望返回至少有一个学生的专业与班级的系不相同的所有文档。请尝试更新后的查询?如果这不符合您的预期,请将预期的响应添加到帖子中。更新后的查询现在返回正确的对象。您知道有没有任何可能的方法可以在不使用聚合函数而使用find函数的情况下执行此操作?我被告知,要找到我的答案的查询应该非常简单,并且可以在没有聚合函数的情况下完成。当然可以。您可以使用
$where
操作符将javascript嵌入
find
函数中。看看这个。。这是基于这个答案的等价物
db.collection.find({“$where”:function(){self=this;return this.students.filter(function(student){return self.department!==student.major;}).length>0})