JSON Collections
A collection is a group of JSON documents that exist within one database. Documents within a collection can have different fields, although, generally all documents in a collection have a similar or related purpose.
To uniquely identify each document in a collection, a document identifier is either specified as id-field in the document or is automatically generated. An index, or primary key, is automatically generated for the unique document identifiers.
db.createCollection("products", {id: {$int}})
db.products1.insert({_id: 1, name: "table", material: "beech"})
db.products2.insert({name: "table", material: "beech"})
When a collection is created implicitly, default settings are used to define the characteristics of the collection. As shown in the examples, the data type of the unique document identifier is determined by the id field in the inserted document.
Due to the flexible nature of the JSON language, the data type of the identifier can be different across multiple documents in the collection. By default, for improved performance, the data type of the unique document identifier is the same for all documents in a collection. However, to accept different data types for the document identifier in a collection, specify the multiTypedID property. For more information, see the topic createCollection() - Creates a collection.
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
- A fixed-size collection that overwrites the entries to make room for the newest information.
- Insertion order is preserved. Therefore, an index is not needed to return objects that are sorted by insertion order.
- Automatically remove the oldest documents without explicit instructions.
- Provides performance benefits for writes and sequential reads.