简体   繁体   English

Mongo DB链式查询

[英]Mongo DB chained query

I am new to MongoDB and as I wonder if a chained query like the following is possible(somewhat like a join): 我是MongoDB的新手,因为我想知道如下链接查询是否可行(有点像连接):

db.places.insert({
"_id": original_id
"place_name": "Broadway Center"
"url": "bc.example.net"})

db.people.insert({
"name": "Erin"
"places_id": original_id
"url":  "bc.example.net/Erin"})

So given a place name string, I want to select the people associated with that place. 因此,给定一个地名字符串,我想选择与该地点相关联的人。 But the people collection only reference the place id, not the place name. 但人们收藏只引用地点ID,而不是地名。

You cannot use joins in MongoDB. 您不能在MongoDB中使用连接。

The idiomatic solution is retrieve all place_ids for that place_name from your places collection and then use the place_ids to query in your people collection. 惯用解决方案是从您的places集合中检索该place_name所有place_ids ,然后使用place_ids在您的人员集合中进行查询。

Another option is keeping, for example, places in people collection (this makes more sense to me than people inside places collection but, of course, it depends on your domain). 另一种选择是保持,例如, placespeople集合(这使我更有意义比people里面places集合,但,当然,这取决于你的域)。 But then you have to take into account that in case that only one place changes, you have to change all people documents sharing a specific place. 但是,您必须考虑到,如果只有一个地方发生变化,您必须更改共享特定地点的所有人员文档。 If people and places are in separate collections this doesn't happen so it depends on if we have static data or not and on if we want to optimize searches or updates. 如果人和地点在不同的集合中,则不会发生这种情况,因此取决于我们是否有静态数据,如果我们想要优化搜索或更新,则取决于是否存在静态数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM