使用elemMatch进行MongoDB查询以从内部对象中匹配嵌套数组数据

时间:2018-08-07 17:28:19

标签: arrays mongodb mongoose mongodb-query mongoid

带有elemMatch的MongoDB查询,用于从内部对象中匹配嵌套数组数据。 在这里,我们有三个项目对象,每个对象都包含一个包含多个对象的考试阵列。 此处,当"project_id" : ObjectId("5b6959e3dc68d21c6613c82d")时,"exams"数组包含两次"writer.user_id":ObjectId("5b6959e3dc68d21c6613c77d") 我怎样才能从第一个项目对象中过滤掉这两个数据?

db.exams.insert([{
"project_id" : ObjectId("5b6959e3dc68d21c6613c82d"),
"exams" : [ 
    {
        "allotment_type" : "manual",
        "exam_id" : 1,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77d"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 1,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 1,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }, 
    {
        "allotment_type" : "manual",
        "exam_id" : 2,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77e"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 2,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 2,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }, 
    {
        "allotment_type" : "manual",
        "exam_id" : 3,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77d"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 3,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 3,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }]

},
{
"project_id" : ObjectId("5b6959e3dc68d21c6613c82e"),
"exams" : [ 
    {
        "allotment_type" : "manual",
        "exam_id" : 1,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77d"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 1,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 1,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }, 
    {
        "allotment_type" : "manual",
        "exam_id" : 2,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77e"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 2,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 2,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }, 
    {
        "allotment_type" : "manual",
        "exam_id" : 3,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77c"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 3,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 3,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }]

},
{
"project_id" : ObjectId("5b6959e3dc68d21c6613c82f"),
"exams" : [ 
    {
        "allotment_type" : "manual",
        "exam_id" : 1,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77d"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 1,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 1,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }, 
    {
        "allotment_type" : "manual",
        "exam_id" : 2,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77e"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 2,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 2,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }, 
    {
        "allotment_type" : "manual",
        "exam_id" : 3,
        "topic" : "something",
        "word_count" : 5,
        "key_word_density" : 2,
        "writer" : {
            "user_id" : ObjectId("5b6959e3dc68d21c6613c77c"),
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "editor" : {
            "user_id" : 3,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        },
        "manager" : {
            "user_id" : 3,
            "versions" : [ 
                {
                    "id" : 1,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 2,
                    "file_path" : "abcd"
                }, 
                {
                    "id" : 3,
                    "file_path" : "abcd"
                }
            ]
        }
    }]

}]);

1 个答案:

答案 0 :(得分:1)

您可以尝试进行$filter聚合

db.collection.aggregate([
  { "$project": {
    "exams": {
      "$filter": {
        "input": "$exams",
        "as": "exam",
        "cond": {
          "$eq": [ "$$exam.writer.user_id", ObjectId("5b6959e3dc68d21c6613c77d") ]
        }
      }
    }
  }}
])