【发布时间】:2016-05-25 19:02:13
【问题描述】:
我想在 MongoDb 上进行查询,例如 Select * from Person, School WHERE Person.pID == School.pID。我怎样才能在 MongoDB 上做到这一点。使用 Scrapy,我已经废弃了这个 page 并且已经有了 Collection,我想在 Collection 和 Mongoose 之间建立外键关系。我怎样才能在这个给定的例子上做到这一点?。谢谢。 (第一个集合是学校,第二个集合是人)
/* School Collection */
{
"_id" : ObjectId("574453fb085516029132205a"),
"involvedInst" : [
"10403"
],
"instID" : [
"10359"
],
"projID" : [
"271289"
],
"fSpeaker" : [],
"pID" : [
"1229134"
],
"speaker" : [
"Professor Dr. Georg Reiser"
]
}
/* PersonCollection */
{
"_id" : ObjectId("574453fb085516029132205a"),
"beteiligteInstituten" : [
"10403"
],
"instID" : [
"10359"
],
"projID" : [
"271289"
],
"fachlicherDFGAnsprechpartner" : [],
"pID" : [
"1229134"
],
"speaker" : [
"Professor Dr. Georg Reiser"
]
}
【问题讨论】:
-
从 3.2 版开始,MongoDB 具有用于聚合的 $lookup 选项 (docs.mongodb.com/manual/reference/operator/aggregation/lookup)。如果您的 MongoDB 版本低于 3.2,您需要生成两个查询并在您的代码中加入。