Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
集合的每个文档中的MongoDB筛选器数组字段_Mongodb_Aggregation Framework - Fatal编程技术网

集合的每个文档中的MongoDB筛选器数组字段

集合的每个文档中的MongoDB筛选器数组字段,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有一个MongoDB集合,其结构如下: /* 1 */ { "_id" : ObjectId("5c09a454dd42360001fd2515"), "conversations" : { "0" : { "message" : "Message 1", . . . }, "1" : { "mess

我有一个
MongoDB
集合
,其结构如下:

/* 1 */
{
    "_id" : ObjectId("5c09a454dd42360001fd2515"),
    "conversations" : {
        "0" : {
            "message" : "Message 1",
              .
              .
              .
        },
        "1" : {
            "message" : "Message 1"
               .
               .
               .
        },
        "2" : {
            "message" : "Message 5"
              .
              .
              .
        },
        "3" : {
            "message" : "Message 1"
              .
              .
              .
        },
        "4" : {
            "message" : "Message 2"
              .
              .
              .
        },
        "5" : {
            "message" : "Message 3"

        },
        "6" : {
            "message" : "compliance"

        },
        "7" : {
            "message" : "Google"

        }
}

/* 2 */
{
    "_id" : ObjectId("5c09a673c2a98f00012f4efb"),

    "conversations" : {
        "0" : {
            "message" : "Message 11"

        },
        "1" : {
            "message" : "Google",

        },
        "2" : {
            "message" : "Message 7"

        }
}

/* 3 */
{
    "_id" : ObjectId("5c09f570173f7900015a82b2"),

    "conversations" : {
        "0" : {
            "message" : "Message 4"
        },

}

.
.
.
在上面的示例数据中,我在
集合
中有一组文档,在每个文档中都有一个名为
对话
的字典字段数组。在此字段中,我有字典字段列表(
0
1
2
…)。我要做的是过滤所有文档,其中在每个
对话
字段中,
消息
值为
消息4

我知道过滤到每个文档中特定字段的方法是
db.getCollection('collection_1')。find({conversation:})
,但我不知道如何将它应用到字典字段列表中,比如我的案例。有什么帮助吗?

您需要操作员将具有未知键的对象转换为
k
v
字段的数组。然后,您可以在该数组中应用,以检查是否有任何
消息
等于
“消息4”


以上答案是否与我的样本数据非常相关?如果我的文档中的对话旁边有其他字段,该怎么办?@user2966197如果您的密钥是动态生成的,并且由于
conv.v
将包含多个字段“消息”,那么您仍然需要使用
$objectToArray
,并且应该仍然可以正常工作。你可以在这里试试:好的。在查询中的何处指定集合名称?我的集合名称是“集合\ 1”,在上面的查询中没有任何地方可以指定它?它会像db.conversation_1.aggregate()一样吗?@user2966197是的,就像您为
.find()所做的那样
db.collection.aggregate([
    {
        $match: {
            $expr: {
                $ne: [
                    {
                        $size: {
                            $filter: {
                                input: { $objectToArray: "$conversations" },
                                as: "conv",
                                cond: { $eq: [ "$$conv.v.message", "Message 4" ] }
                            }
                        }
                    },
                    0
                ]
            }
        }
    }
])