使用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"
}
]
}
}]
}]);