mongoDb 中的查询和分组?
第 1 部分:
我有(学生)集合:
{
sname : "",
studentId: "123"
age: "",
gpa: "",
}
我试图从中仅获取两个密钥:
{
sname : "",
studentId: "123"
}
所以我需要消除年龄和 gpa 以便只有姓名和学生 ID,我该怎么做?
第2部分:
然后我有“主题”集合:
{
subjectName : "Math"
studentId : "123"
teacherName: ""
}
我需要将前面的键(第1部分中)与正确的studentId进行匹配/组合,这样我最终会得到这样的结果:
{
sname : "",
studentId: "123",
subjectName : "Math"
}
我该如何做到这一点并且这是获得结果的正确思考方式吗?我尝试阅读有关 group 和 mapReduce 的内容,但没有找到明确的示例。
Part 1:
I have (student) collection:
{
sname : "",
studentId: "123"
age: "",
gpa: "",
}
im trying to get only two keys from it :
{
sname : "",
studentId: "123"
}
so i need to eliminate age and gpa to have only name and studentId , how could i do that ?
Part2:
Then I have 'subject' collection :
{
subjectName : "Math"
studentId : "123"
teacherName: ""
}
I need to match/combine the previous keys (in part1) with the correct studentId so I will end up with something like this :
{
sname : "",
studentId: "123",
subjectName : "Math"
}
How can i do this and is that the right way to think to get the result? i tried to read about group and mapReduce but i didnt find a clear example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答您的第一个问题,您可以这样做:
其中的第一个 {} 是限制查询,在本例中包括整个集合。后半部分用 1 或 0 指定键,具体取决于您是否想要它们回来。但不要在单个查询中混合包含和排除。除了一些特殊情况外,mongo 不会接受它。
你的第二个问题比较困难。您要求的是加入,而 mongo 不支持这一点。无法连接 StudentId 上的两个集合。您需要找到所有想要的学生,然后使用这些学生 ID 来查找所有匹配的科目。然后,您需要将两个结果合并到您自己的代码中。您可以通过您使用的任何驱动程序来执行此操作,也可以在 shell 本身的 javascript 中执行此操作,但无论哪种方式,您都必须将它们与您自己的代码合并。
编辑:
下面是一个示例,说明如何在 shell 中执行此操作,并将输出发送到名为“out”的集合。
如果这不是经常更改的数据,您可以删除“out”集合并使用此 shell 脚本定期重新填充它。然后你的代码可以直接从“out”查询。如果数据确实经常更改,您将需要在代码中动态进行合并。
另一种可能更好的选择是将“主题”数据包含在“学生”集合中,反之亦然。这将产生更加 mongodb 友好的结构。如果您经常遇到这种连接问题,mongo 可能不是最佳选择,关系数据库可能更适合您的需求。
To answer your first question, you can do this:
The first {} in that is the limiting query, which in this case includes the entire collection. The second half specifies keys with a 1 or 0 depending on whether or not you want them back. Don't mix include and excludes in a single query though. Except for a couple special cases, mongo won't accept it.
Your second question is more difficult. What you're asking for is a join and mongo doesn't support that. There is no way to connect the two collections on studentId. You'll need to find all the students that you want, then use those studentIds to find all the matching subjects. Then you'll need to merge the two results in your own code. You can do this through whatever driver you're using, or you can do this in javascript in the shell itself, but either way, you'll have to merge them with your own code.
Edit:
Here's an example of how you could do this in the shell with the output going to a collection called "out".
If this isn't data that changes all that often, you could just drop the "out" collection and repopulate it periodically with this shell script. Then your code could query directly from "out". If the data does change frequently, you'll want to do this merging in your code on the fly.
Another, and possibly better, option is to include the "subject" data in the "student" collection or vice versa. This will result in a more mongodb friendly structure. If you run into this joining problem frequently, mongo may not be the way to go and a relational database may be better suited to your needs.
Mongo 的
find()
运算符允许您在结果中包含或排除某些字段查看 字段选择 了解更多信息。您可以执行以下任一操作:
为了将您的学生和科目联系在一起,您可以分别查找学生有哪些科目,如下所示:
或者将科目数据嵌入到每个学生中,并将其与学生文档一起检索:
Mongo's
find()
operator lets you include or exclude certain fields from the resultsCheck out Field Selection in the docs for more info. You could do either:
For relating your student and subject together, you could either lookup which subjects a student has separately, like this:
Or embed subject data with each student, and retrieve it together with the student document: