MongoDb is a document database (i.e. a no SQL database).

It is advertised as cross-platform, providing high performance, high availability, and easy scalability.


MongoDb is a schemaless database. Every document can be a different structure, if you want.


MongoDb does not support relationships between Collections or Documents.
If you want data to be used together, save it as one document.

Instead of relying on foreign keys, just duplicate the data whereever it is needed.
MongoDb says disk space is cheap.

A database contains multiple Collections.

Each database on a server has it's own section of the file system.

A collection contains multiple Documents.

A collection is analogous to a SQL Table.

Dynamic Schema

MongoDb Collections do not enforce a schema, meaning that documents in the same collection do not have to have the same fields.
Fields with the same Key do not need to contain the same data types.


What to divide into multiple Collections, and what to keep in one Collection?

Put multiple types in one Collection:
- Collections cannot be joined for queries.
- Aggregations cannot be performed across multiple Collections.

One type per Collection:
- To deserialize, you must know what type you are dealing with.
- If mixing types in a Collection, make sure they can be easily differentiated. Maybe by a "type" field.
- Indexes on the Collection will be updated whether or not the new/edited records contains the field being indexed.
- If the Indexed fields are all shared fields, that's an indication these types should be in the same Collection.
- If only part of a Collection is frequently updated, it should probably be its own Collection. Otherwise you'll get locking conflicts.
- Ex: An "auction item" has an array of "bids" under it. Now it's hard for people to all save their bids because they lock each other out of the database.

Many suggestions online says don't nest data more than 1 level deep. But that probably depends on how variable the structure is and how precise of a programmer you are.
- Ex: Don't have a array of objects which also contain arrays. At least, not when you'll want to search by those value.

A document contains multiple Key/Value Pairs called Fields.

A document is analogous to a SQL Row/Record.


Documents are displayed, edited, etc as JSON objects.


    _id: ObjectId(7df78ad8902c),
    title: 'Test',
    comments: [
            user: 'Steve',
            comment: 'Test comment'

Default Key

MongoDb provides a default key Field called "_id".
You can specify the _id when inserting a record, or allow MongoDb to generate it.
The default _id is made up of the timestamp, machine id, process id, and sequence number.


Find will return a list of records.

Where field equals X:

db.getCollection('MyCollection').find({"myField": "X"})
db.getCollection('MyCollection').find({"myObject.myField": "X"})
db.getCollection('MyCollection').find({"myField": UUID("12345678-1234-1234-1234-123456789012")})

Where field exists:

db.getCollection('MyCollection').find({"myObject.myField":{"$exists": true}})
If any link in the path to the field does not exist, then the field does not exist.


Count results (returns integer):

db.getCollection('MyCollection').find({"myField": "X"}).count()