MongoDB is the document database designed to make it easy for developers to work with data in any form, and from any programming language. Whether you’re just firing up your first MongoDB Atlas cluster, or you’re a long-time veteran user, we put together our best set of useful examples to refresh your knowledge or help you get your bearings. Don’t hesitate to go over to the official documentation for more in-depth discussions of all of these topics.
Structuring Document Data
MongoDB documents are formatted in BSON (an extended Binary form of JSON) , which allows you ultimate flexibility in structuring data of all types. Below are just a few of the ways you can structure your documents.
Storing Nested Data Structures
Perhaps the most powerful feature of document databases is the ability to nest objects inside of documents. A good rule of thumb for structuring data in MongoDB is to prefer embedding data inside documents to breaking it apart into separate collections, unless you have a good reason (like needing to store unbounded lists of items, or needing to look up objects directly without retrieving a parent document).
Note that the name field is a nested object containing both given and family name components, and that the addresses field stores an array containing multiple addresses. Each address can have different fields in it, which makes it easy to store different types of data.