2

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?

4

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.

1

Document in MongoDB is analogous to row in relational database. As opposed to relational database, MongoDB supports dynamic schema i.e. documents can consist of varying fields.

Collection is a set of documents belonging to the same entity similar to tables in relational databases.

0

Collection can be visualized as a table in RDBMS.

Document can be visualized as a row in RDBMS.

Biggest difference is collection need not have all the fields/columns.

There is no joining of two columns. It is achieved by nested documents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.