MongoDB Mongoosess
MongoDB Mongoosess
Web Architecture
MongoDB
MongoDB is an open-source, NoSQL database that
uses a JSON-like (BSON) document-oriented
model.
mongoose.connect('mongodb://localhost/myproject');
Simple index
first_name: {type: 'String', index: true}
Index with unique enforcement
user_name: {type: 'String', index:
{unique: true} }
Defaults
date: {type: Date, default: Date.now }
Secondary Indexes
Performance and space trade-off
- Faster queries: Eliminate scans - database just returns
the matches from the index
- Slower mutating operations: Add, delete, update must
update indexes
- Uses more space: Need to store indexes and indexes
can get bigger than the data itself
When to use
- Common queries spending a lot of time scanning
- Need to enforce uniqueness
Make model from schema
A Model in Mongoose is a constructor of objects a
collection May or may not correspond to a model of the
MVC
Sorting
query.sort("first_name").exec(doneCallback);
Limits
query.limit(50).exec(doneCallback);
query.sort("-location")
Deleting objects from
collection
Client-server
Client-view or front-end
User Model
View Controller
user action
View Controller
user action
View Controller
user action