Sharding and reference

Hi guys, the question is simple: can I use reference (aka foreign key for SQL-like db) in my database with sharding?

I found this article What is MongoDB Sharding and the Best Practices? that says:

Since there’s no support in databases to point to entities outside the current database (well, even a different database on the same machine is not supported, so forget about a database on a different machine), the concept of foreign keys goes for a toss as well. Suddenly, the database becomes “dumb,” and data integrity is your problem.

But I haven’t found other sources regarding this topic, not even on the official documentation.

Thank u :blush:

Hi @Elia_Gaviraghi and welcome in the MongoDB Community :muscle: !

From the 6.0 document (upcoming version of MongoDB).

Starting in MongoDB 5.1, $lookup works across sharded collections.

So as long as your related data are in the same database (which makes sense), you can use $lookup to join them together in a query and manipulate the data as you please in your aggregation pipeline.

Cheers,
Maxime.

1 Like

Excellent news! I am currently using 5.0.8 community edition, I’ll wait for the official release to upgrade.
Thank u so much Maxime :blush:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.