

In this case, MongoDB will ensure that no conflict occurs and only one set of data is updated at a time.
#Mongodb compass join two collections update
When multiple users are accessing the data, there's always a chance that two or more users will try to update the same document with different data. Document updates to a single document are always atomic, but not for multiple documents. Rather than writing complex queries that join many documents via their unique identifier, you can return all related data within a single query.Īnother consideration to keep in mind is that, MongoDB can only ensure atomicity at a document level. This can provide a major performance boost - especially when working with lots of data.Įmbedded relationships also make queries easier to write. MongoDB only needs to return the one document, rather than joining multiple documents in order to retrieve the relationships. When the relationship is embedded within the document, queries will run faster than if they were spread out over multiple documents. One of the main benefits of using the embedded relationship method is performance. There are times you might use embedded documents, and other times you'll use referenced documents. When to use Embedded Documents vs Referenced Documentsīoth methods of creating relationships have their pros and cons.
#Mongodb compass join two collections code
The following code creates a one-to-one relationship, embedded within the document.ĭb.artists.find( One-to-One RelationshipĪ one-to-one relationship is where the parent document has one child, and the child has one parent.įor example, a business rule might say that an artist can only have one address and that the address can only belong to one artist. In fact, we already created a relationship using this method back when we first created a document. Therefore, a single document can contain its own relationships. With MongoDB, you can embed documents within documents.

The method you use will depend on the data, and how you intend to query that data. In MongoDB, you can create a relationship using one of the following two methods: MongoDB databases work differently to relational databases. To create a relationship in MongoDB, either embed a BSON document within another, or reference it from another.
