Are documents and collections like objects?

tdd

#1

Question

Are documents and collections like objects?

Answer

As we start exploring working with Mongoose in this lessons, we see that it refers to each set of key/value pairs as a document, and a set of documents is called a collection. What are this two exactly?

Both are stored in the MongoDB database, according to MongoDB documentation:

collection

A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose. See Namespaces.

Lets clear some of that up, what is an RDBMS table? RDBMS stands for Relational Database Management System, which is your regular SQL, where you have an index based system (id) to relate to each item inserted and they all have to have the same properties (column names). A visual representation would be a dogs table:

id | name | breed| age


1 | “fluffy” | “poodle” | 3
2| “spike”| “bulldog”| 2

etc.
A collection is equivalent, but it does not enforce a schema, which means that if we were to make a collection of dogs, they do not have to all have a name, they certainly will not be tracked by id, they may have different properties, not just name, breed, and age, and the values can be entered in any order. Collections give us the flexibility to only focus on having dogs in a dogs table, that is if we want all documents to have a similar purpose. this leads us to:

document

A record in a MongoDB collection and the basic unit of data in MongoDB. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.

So what is JSON and BSON?
JSON stands for JavaScript Object Notation that most commonly contain key/value data in string form:

{
  "Dogs" : [
    { "id": "1",
      "name": "fluffy",
      "breed": "poodle",
      "age": "3"
    },
    {...}
  ]
}

BSON, on the other side, is a Binary encoded JSON , here is an example I found for a hello world key/value pair:

 {"hello": "world"} →	

  \x16\x00\x00\x00                   // total document size
  \x02                               // 0x02 = type String
  hello\x00                          // field name
  \x06\x00\x00\x00world\x00          // field value
  \x00                               // 0x00 = type EOO ('end of object')

 {"BSON": ["awesome", 5.05, 1986]}
→	
  \x31\x00\x00\x00
  \x04BSON\x00
  \x26\x00\x00\x00
  \x02\x30\x00\x08\x00\x00\x00awesome\x00
  \x01\x31\x00\x33\x33\x33\x33\x33\x33\x14\x40
  \x10\x32\x00\xc2\x07\x00\x00
  \x00
  \x00

Because of the simplification of turning the objects into binary, it becomes space savvy in most cases.

Now we know, a document is a JSON object structured in binary, and a collection is like a representational database management system, but with greater flexibility, like not needing to have a specific schema to follow for each document we add to it.