Gank
Gank

Reputation: 4677

Mongodb Is there a plugin to query multi collection like "join"?

Mongodb doesn't support multi collection query, like "left join" in SQL. It only support "populate", but it can't populate subdocument and find parent-document at the same time.

One line query code in SQL, while mongodb user has to query every parent document _id himself.

I have run into these question: How to populate other collection's subdocument in mongoose?

https://stackoverflow.com/questions/24075910/mongoose-cant-update-or-insert-subdocuments-array

I finally query every _id by myself in a forEach loop.

Is there a plugin to query multi collection like "join"? Or is there any better solution to multi collection query?

Upvotes: 1

Views: 357

Answers (1)

Gates VP
Gates VP

Reputation: 45307

Is there a plugin to query multi collection like "join"?

Some DB products have plug-ins that provide extra features, like PostGIS for Postgres. MongoDB does not have such a plug-in for JOINs. It is unlikely that such a plug-in will ever exist because MongoDB is designed not to have join support.

mongoose...

So the one spot where there is "join-like" support are the drivers. Some drivers and wrappers (like Morphia), have support for opening a document and its related sub-documents from a different collection. However, in this case, the driver/tool is simply doing the work of performing multiple queries on your behalf.

This can easily generate too many queries.

Or is there any better solution to multi collection query?

The only solution provided wholly within MongoDB is going to be via the Map / Reduce or Aggregation Framework tools. Even with these tools, you are likely going to have to do multiple passes of the data and then write some scripts to stitch together this data. This will be a lot of work, but you're trying to do something that MongoDB doesn't like to do, so that's expected.

Another solution would be to leverage Hadoop. MongoDB has a Hadoop plug-in so you can run Hadoop over the existing data. Add in some Hadoop query tools like Hive and then you can get an SQL-Like query over the top. This will also be a lot of work, but will enable to run all sorts of SQL-like queries.

Upvotes: 1

Related Questions