MongoDB从数组返回内部文档
我试图从文档中的数组中获取一个元素,而只获取我不想要整个文档的元素MongoDB从数组返回内部文档,mongodb,mongodb-query,aggregation-framework,projection,Mongodb,Mongodb Query,Aggregation Framework,Projection,我试图从文档中的数组中获取一个元素,而只获取我不想要整个文档的元素 db.dept.find({"section.classes.CRN":"1901"}).limit(100) db.dept.where("section.classes.CRN").eq("1901").limit(100) json { "_id" : ObjectId("5d70ab0c280d6b8ebb850cc1"), "name" : "Art Studio", "abbr" : "AR
db.dept.find({"section.classes.CRN":"1901"}).limit(100)
db.dept.where("section.classes.CRN").eq("1901").limit(100)
json
{
"_id" : ObjectId("5d70ab0c280d6b8ebb850cc1"),
"name" : "Art Studio",
"abbr" : "ARS",
"section" : [
{
"type" : "Undergraduate Courses",
"classes" : [
{
"CRN" : "193",
"Course" : "ARS100",
"Sec" : "01",
"Title" : "Drawing I",
"Cr" : "3",
"Dates" : "8/26-12/19",
"Days" : "MR",
"Time" : "1230P-0320P",
"Loc" : "SAB 226",
"Instructor" : "Schuck",
"Attributes" : "",
"Avail" : "F"
},
{
"CRN" : "293",
"Course" : "ARS100",
"Sec" : "02",
"Title" : "Drawing I",
"Cr" : "3",
"Dates" : "8/26-12/19",
"Days" : "MR",
"Time" : "0330P-0620P",
"Loc" : "SAB 226",
"Instructor" : "Itty",
"Attributes" : "",
"Avail" : "F"
},
{...
我尝试了另一种方法,但他们都返回了整个文档
db.dept.find({"section.classes.CRN":"1901"}).limit(100)
db.dept.where("section.classes.CRN").eq("1901").limit(100)
json
{
"_id" : ObjectId("5d70ab0c280d6b8ebb850cc1"),
"name" : "Art Studio",
"abbr" : "ARS",
"section" : [
{
"type" : "Undergraduate Courses",
"classes" : [
{
"CRN" : "193",
"Course" : "ARS100",
"Sec" : "01",
"Title" : "Drawing I",
"Cr" : "3",
"Dates" : "8/26-12/19",
"Days" : "MR",
"Time" : "1230P-0320P",
"Loc" : "SAB 226",
"Instructor" : "Schuck",
"Attributes" : "",
"Avail" : "F"
},
{
"CRN" : "293",
"Course" : "ARS100",
"Sec" : "02",
"Title" : "Drawing I",
"Cr" : "3",
"Dates" : "8/26-12/19",
"Days" : "MR",
"Time" : "0330P-0620P",
"Loc" : "SAB 226",
"Instructor" : "Itty",
"Attributes" : "",
"Avail" : "F"
},
{...
在搜索一组CRN值时,我试图得到这个或类似的结果
json
[ {
"CRN" : "193",
"Course" : "ARS100",
"Sec" : "01",
"Title" : "Drawing I",
"Cr" : "3",
"Dates" : "8/26-12/19",
...
"Instructor" : "Schuck",
"Attributes" : "",
"Avail" : "F"
}
]
在mongodb中称为projection
,在find查询中传递第二个对象以指定要在结果中显示哪些字段
所以,根据上面的例子,如果您想要结果中的名称和节,您应该传递类似的内容
db.dept.find({"section.classes.CRN":"1901"},{"name":1, "section":1}).limit(100)
在mongodb中称为projection
,在find查询中传递第二个对象以指定要在结果中显示哪些字段
所以,根据上面的例子,如果您想要结果中的名称和节,您应该传递类似的内容
db.dept.find({"section.classes.CRN":"1901"},{"name":1, "section":1}).limit(100)
尝试使用聚合管道将双嵌套数组投影为: 输入:
[
{
"_id": ObjectId("5d70ab0c280d6b8ebb850cc1"),
"name": "Art Studio",
"abbr": "ARS",
"section": [
{
"type": "Undergraduate Courses",
"classes": [
{
"CRN": "193",
"Course": "ARS100",
"Sec": "01",
"Title": "Drawing I",
"Cr": "3",
"Dates": "8/26-12/19",
"Days": "MR",
"Time": "1230P-0320P",
"Loc": "SAB 226",
"Instructor": "Schuck",
"Attributes": "",
"Avail": "F"
},
{
"CRN": "293",
"Course": "ARS100",
"Sec": "02",
"Title": "Drawing I",
"Cr": "3",
"Dates": "8/26-12/19",
"Days": "MR",
"Time": "0330P-0620P",
"Loc": "SAB 226",
"Instructor": "Itty",
"Attributes": "",
"Avail": "F"
}
]
}
]
}
]
查询:
在展开部分,您可以为CRN筛选类
db.collection.aggregate([
{
$unwind: "$section"
},
{
$project: {
name: 1,
abbr: 1,
"section.type": 1,
"section.classes": {
$filter: {
input: "$section.classes",
as: "item",
cond: {
$eq: [
"$$item.CRN",
"193"
]
}
}
}
}
},
{
$group: {
_id: "$_id",
section: {
$push: "$section"
}
}
}
])
输出:
您可以在project中根据需要管理密钥,以添加新密钥或替换密钥
[
{
"_id": ObjectId("5d70ab0c280d6b8ebb850cc1"),
"section": [
{
"classes": [
{
"Attributes": "",
"Avail": "F",
"CRN": "193",
"Course": "ARS100",
"Cr": "3",
"Dates": "8/26-12/19",
"Days": "MR",
"Instructor": "Schuck",
"Loc": "SAB 226",
"Sec": "01",
"Time": "1230P-0320P",
"Title": "Drawing I"
}
],
"type": "Undergraduate Courses"
}
]
}
]
尝试使用聚合管道将双嵌套数组投影为: 输入:
[
{
"_id": ObjectId("5d70ab0c280d6b8ebb850cc1"),
"name": "Art Studio",
"abbr": "ARS",
"section": [
{
"type": "Undergraduate Courses",
"classes": [
{
"CRN": "193",
"Course": "ARS100",
"Sec": "01",
"Title": "Drawing I",
"Cr": "3",
"Dates": "8/26-12/19",
"Days": "MR",
"Time": "1230P-0320P",
"Loc": "SAB 226",
"Instructor": "Schuck",
"Attributes": "",
"Avail": "F"
},
{
"CRN": "293",
"Course": "ARS100",
"Sec": "02",
"Title": "Drawing I",
"Cr": "3",
"Dates": "8/26-12/19",
"Days": "MR",
"Time": "0330P-0620P",
"Loc": "SAB 226",
"Instructor": "Itty",
"Attributes": "",
"Avail": "F"
}
]
}
]
}
]
查询:
在展开部分,您可以为CRN筛选类
db.collection.aggregate([
{
$unwind: "$section"
},
{
$project: {
name: 1,
abbr: 1,
"section.type": 1,
"section.classes": {
$filter: {
input: "$section.classes",
as: "item",
cond: {
$eq: [
"$$item.CRN",
"193"
]
}
}
}
}
},
{
$group: {
_id: "$_id",
section: {
$push: "$section"
}
}
}
])
输出:
您可以在project中根据需要管理密钥,以添加新密钥或替换密钥
[
{
"_id": ObjectId("5d70ab0c280d6b8ebb850cc1"),
"section": [
{
"classes": [
{
"Attributes": "",
"Avail": "F",
"CRN": "193",
"Course": "ARS100",
"Cr": "3",
"Dates": "8/26-12/19",
"Days": "MR",
"Instructor": "Schuck",
"Loc": "SAB 226",
"Sec": "01",
"Time": "1230P-0320P",
"Title": "Drawing I"
}
],
"type": "Undergraduate Courses"
}
]
}
]
我试过了,查询返回整个文档,我只想返回单个课程,如->{“CRN”:“193”,“课程”:“ARS100”,…}而不是父信息。我试过了,查询返回整个文档,我只想返回单个课程,如->{“CRN”:“193”,“课程”:“ARS100”,…}不是家长信息。pip可以工作,但我也可以在集合中获得其他文档:{“_id”:ObjectId(“5d70bb69a1876ad9dd995882”),“section”:..{“_id”:ObjectId(“5d70bb69a1876ad9dd995884”),“section”:…您是否只希望类数组响应一个json或所有json文档,但我有多个文档,而不仅仅是{“\u id”:ObjectId(“5d70ab0c280d6b8ebb850cc1”)}。我只需要一个出现CRN列表的数组。这将给出一个类数组,其中所有文档“CRN”=“193”,尝试上面的查询,它将为您提供所需的输出pip可以工作,但我也可以在集合中获得其他文档:{“id”:ObjectId(“5d70bb69a1876ad9dd995882”),“section”:..{“id”:ObjectId(“5d70bb69a1876ad9dd995884”),“section”:…您是否只希望类数组响应一个json或所有json文档,但我有多个文档,而不仅仅是{“\u id”:ObjectId(“5d70ab0c280d6b8ebb850cc1”)}。我只需要一个出现CRN列表的数组。这将给出一个类数组,其中所有文档“CRN”=“193”,尝试上面的查询,它将为您提供所需的输出