如何在$ filter mongodb中比较数组字段和数组输入值

时间:2017-02-19 10:46:30

标签: mongodb mongodb-query aggregation-framework

我愿意在这个系列中应用$ filter来获得#34; Applied Mathematics"结果只在数组中。我正在提供像这样[" Applied Mathematics"]的数组形式的输入到下面的集合。

 $filter: {
            input: "$univisorEducation",
            as: "univisorEducation",
            cond: {
                $setIsSubset: ["$$univisorEducation.course", courses]
            }
        }

为此我在$ project中使用$ filter,下面是我的$ filter代码

{ "_id" : ObjectId("58a95d81eead32e82932b535"), "univisorEducation" : [ ] }
{
    "_id" : ObjectId("58a9643deead32e82932b54b"),
    "univisorEducation" : [
        {
            "educationLevel" : "MASTER",
            "courseType" : "GRADUATE",
            "year" : 2020,
            "college" : ObjectId("58a936afd48f2b502858b9f7"),
            "_id" : ObjectId("58a96495eead32e82932b54f"),
            "course" : [
                "Applied Mathematics"
            ]
        }
    ]
}

问题是我只得到了这个结果

 <android.support.v7.widget.Toolbar
            android:id="@+id/videos_toolbar"
            style="@style/ToolBarTextStyle"
            android:layout_width="match_parent"
            android:layout_height="?android:actionBarSize"
            android:layout_alignParentTop="true"
            android:background="@color/ToolBarColor"
            android:paddingRight="@dimen/_32sdp"
            android:textAlignment="center"
            app:titleTextColor="@color/ThemeColor">

&#34; _id&#34; :ObjectId(&#34; 58a95d81eead32e82932b535&#34;)应该有应用数学的结果。

1 个答案:

答案 0 :(得分:1)

将您的$filter汇总更改为以下内容。

courses数组与univisorEducation.course进行比较,如果匹配univisorEducation.course则返回true,否则返回false。

$filter: {
    input: "$univisorEducation",
    as: "univisorEducation",
    cond: {
        $ne:[{$setIntersection: [courses, "$$univisorEducation.course" ]}, []]
    }
}