Seven NoSQL Databases in a Week
上QQ阅读APP看书,第一时间看更新

MongoDB collections

The collection is a container for MongoDB documents. It is equivalent to SQL tables, which store the data in rows. The collection should only store related documents. For example, the user_profiles collection should only store data related to user profiles. It should not contain a user's friend list as this should not be a part of a user's profile; instead, this should fall under the users_friend collection.

To create a new collection, you can use the following command:

Here, db represents the database in which we are storing a collection and users_profile is the new collection we are creating.

Documents in a collection should have a similar or related purpose. A database cannot have multiple collections with the same name, they are unique in the given database.

Collections do not force the user to define a schema and are thus known as schemaless. Documents within the collection have different fields. For example, in one document, we can have user_address, but in another document, it is not mandatory to have the user_address field.

This is suitable for an agile approach.