Helps me to handle operations with mongoDB
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
Is possible to add values after creating a schema whit the method add
const CamiloSchema = new Schema();
CamiloSchema.add({ name: 'string', color: 'string', price: 'number' });
Model is a class that's your primary tool for interacting with MongoDB. An instance of a Model is called a Document.
///////////////////////Module Name///Schema
var myModel = mongoose.model("link", new mongoose.Schema({name: String}));
Documents have many of their own built-in instance methods. We may also define our own custom document instance methods too.
- Examples
- Find:
Model.find({properties})
Model.findBy({properties})
: Returns a fake promise, inorder to execute it is necessary add .exec()Model.findById({properties})
.exec(): Returns a promiseModel.findByIdAndUpdate({properties}
, {dataToUpdate}, {new: true}).exec(): {new: true} it's necessary in order to return the new updated object, if not the retuned obj it's going to be the old oneModel.findOneAndRemove({properties})
-
Match equal value
model.find({ property: {$eq: value }})
- Docs
-
Select documents where the value is greater than (>)
-
Select documents where the value is greater or equal than (>=)
-
Matches any of the values specified in an array.
-
Matches values that are less than a specified value.
-
Matches values that are less than or equal to a specified value.
-
Matches all values that are not equal to a specified value.
-
Matches none of the values specified in an array.
There are many other useful operators that can make our life easy, short list:
- Logical: $and, $or, $not, $nor
- Elemet: $exist, $type
- Evaluation: $expr, $jsonSchema, $mod, $regex, $text, $where
- Array: $all, $elemMatch, $size
- Comments: $comments
- Projection: $, $elemtMatch, $slice, $meta
- Field: $inc, $min, $max, $mul, $rename, $set, $setOnInsert, $unset
- Array Update Operators: $addToset, $pop, $pull, $push, $pullAll
- Array Update Operator Modifiers: $each, $position, $slice, $sort
Some times is necessary to add new files on our document, limit the outcomes or group it, this is the Documentation
These methods modify the way that the underlying query is executed. Documentation
model.find({ staff: {$in: ["d", "f"]} }).limit(2).exec();
model.find({ staff: {$in: ["b", "c"]} }).sort("+students").exec();
model.find({ staff: {$in: ["b", "c"]} }).sort("-students").exec();
model.find({ staff: {$in: ["b", "c"]} }).sort({students: 1}).exec();
model.find({ staff: {$in: ["b", "c"]} }).sort({students: -1}).exec();
###Query Helpers You can also add query helper functions, which are like instance methods but for mongoose queries. Query helper methods let you extend mongoose's chainable query builder API.
####Virtuals Virtuals are document properties that you can get and set but that do not get persisted to MongoDB. The getters are useful for formatting or combining fields, while setters are useful for de-composing a single value into multiple values for storage.
If you use toJSON() or toObject() mongoose will not include virtuals by default. This includes the output of calling JSON.stringify() on a Mongoose document, because JSON.stringify() calls toJSON(). Pass { virtuals: true } to either toObject() or toJSON().
- Definition
- Execution: every time we make a request through a Schema with a virtual definition that value is going to be returned by default
Middleware (also called pre and post hooks) are functions which are passed control during execution of asynchronous functions.
- 200- OK; Standard response for successful HTTP requests
- 201- Created; Request has been fulfilled. New resource created
- 204- No Content; Request processed. No content returned
- 301- Moved Permanently; This and all future requests directed to the given URI
- 304- Not Modified; Resource has not been modified since last requested
- 400- Bad Request; Request cannot be fulfilled due to bad syntax
- 401- Unauthorized; Authentication is possible, but has failed
- 403- Forbidden; Server refuses to respond to request
- 404- Not Found; Requested resource could not be found
- 500- Internal Server Error; Generic error message when server fails
- 501- Not Implemented; Server does not recognize method or lacks ability to fulfill
- 503- Service Unavailable; Server is currently unavailable