gpt4 book ai didi

mongodb - 如何在mongoDB中加入数据库并获取所需结果

转载 作者:行者123 更新时间:2023-12-03 15:59:40 24 4
gpt4 key购买 nike

我有一些以下类型的数据库-

var school= mongoose.model('School' ,{
name : { type : String, minlength : 1, trim : true },
email : { type : String, trim : true },
password : { type : String, minlength : 8, },
mobile : { type : Number, minlength : true },
address : { type : String },
description : { type : String },
student : { type : Array },
});


我的网站上有很多学校,所以学生应该只能看到同一所学校的学生。为此,我为学生创建了一个不同的表,并将他们的唯一值与相应的学生数据库一起输入到School DB的Student Array中。如果我有来自School DB的学生数组,我应该如何获取所有Student数据。

最佳答案

您应该首先在学校数据库中查询在学生数组字段中具有特定ID的文档。这是一个简单的查找查询,您可以在此查询学生字段的ID。它将在整个数组中搜索每个文档。

school.findOne({ student : id});


一旦有了特定的学校文件,就可以使用其学生字段来包含所有学生的数组。在这里,您可以使用$ in运算符查询(查找),然后将所有学生作为数组返回。

https://docs.mongodb.com/manual/reference/operator/query/in/

关于mongodb - 如何在mongoDB中加入数据库并获取所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52019370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com