带有 elemMatch 的 MongoDB 查询,用于从对象内部匹配嵌套数组数据 [重复]
Posted
技术标签:
【中文标题】带有 elemMatch 的 MongoDB 查询,用于从对象内部匹配嵌套数组数据 [重复]【英文标题】:MongoDB query with elemMatch for nested array data matching from inside objects [duplicate] 【发布时间】:2019-01-14 20:57:54 【问题描述】:使用 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】:你可以试试$filter
聚合
db.collection.aggregate([
"$project":
"exams":
"$filter":
"input": "$exams",
"as": "exam",
"cond":
"$eq": [ "$$exam.writer.user_id", ObjectId("5b6959e3dc68d21c6613c77d") ]
])
【讨论】:
谢谢。完美的解决方案。 我们可以使用 $elemMatch 做同样的事情吗?$elemMatch
总是返回第一个匹配元素而不是所有匹配元素
感谢您的有效重播。也请帮助我! ,如果我需要传递 project_id 我应该在哪里传递?像另一种情况 project_id= ObjectId("")
嗨 Anthony Winzlet,我是 MongoDB 的初学者。我发现我们可以将 $match 数据与聚合一起使用。 $match:"project_id" : ObjectId("5b6959e3dc68d21c6613c82d") ,再次非常感谢..以上是关于带有 elemMatch 的 MongoDB 查询,用于从对象内部匹配嵌套数组数据 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
mongodb 中的Multikey Index Bounds解释$elemMatch
MongoDB:$ elemMatch和$ and和在数组内查找对象之间有什么区别?
MongoDB (Mongoose) 如何使用 $elemMatch 返回所有文档字段