Mongodb – considered as a document in mongoDB

mongodbnosql

I have been learning MongoDB for the past two days and I've been confused with documents and their limits, and how to overcome the limits.

What is a difference between documents and collection?

Best Answer

In the context of MongoDB, a document means any piece of valid BSON* (binary JSON). The word does not have the everyday meaning of, say, an MS Word file or PDF.

A collection is a container for zero or more documents. There is no requirement for all documents in a collection to have the same structure. Indeed, that is one of the drivers behind the NoSQL movement. So it would be possible to store a shopping cart, a birth certificate and a recipie for chocolate cake in the same collection. (Though possible it would be an exceedingly poor design.) A collection is also the boundary for some management tasks, like copying, and authorisation.


* BSON is actually an important distinction as there are more data types than JSON. For example, the default primary key (_id field) is an ObjectId, which doesn't exist as a standard JSON type.

For more context see:

BSON and MongoDB Extended JSON are JSON-like in terms of document structure, but definitely not interchangeable.