Mongoose Packet Operation MongoDB

Source: Internet
Author: User

Reading this article requires a basic knowledge of MongoDB in Nodejs to operate the MONGO database first understand a few concepts: MongoDB and MONGOOSEMONGODB package is a driver to provide a program to operate the MongoDB database Mongoose package is based on MongoDB built an object operation Model, which is suitable for the more important three concepts in Nodejsmongoose: Schema, which defines the structure of the database, such as the data type of key, what Key,key value values are in the document, etc. Schema does not have the ability to manipulate databases. Model, a constructor compiled by the schema, used to define the collection model, and the model instance is the document. Model with database additions and deletions to check. Entity, which is a document created by the model constructor. var mongoose = require (' Mongoose ') connection database Usermongoose.connect ("mongodb://127.0.0.1:27017/user") definition Schemavar Userschema = new Mongoose. Schema ({id:number, Name:{reuired:true,default: ' Undefined ', match:/a/}, Age:number}) schema can also refine other properties of key, i.e. document validation, document The validated document will not be saved, but will not be saved anyway. Type: Data type, such as number,stringrequired:true data must be filled out, false non-mandatory data: Default value Validate: Custom match, value is Custom function, return value is Boolean, True passed, False does not pass min: min (numeric only) Max: maximum (numeric only) match: Regular match (for string only) Enum: enumeration match (for strings only) define model, where Testco is the set name var Usermodel =                Mongoose.model (' Testco ', Userschema) generates Entityvar doc = new Usermodel ({id:1, name: ' AA ', AGE:23}) The custom schema requires new Mongoose. ScHema () model is called Mongoose.model () custom entity requires New model () in the shell background of MONGO, the operation method is basically in the set down, mongoose is so, the role of the model to act as a collection , nature is also under it to manipulate the database. Reference link: Http://www.nodeclass.com/api/mongoose.html#model-js model.create (Doc (s), (Err,doc (s)) =>{}) Eg.userModel.create ({id:2,name: "BB", Age:33},{id:3,name: "CC", Age:3}, (ERR,DOC1,DOC2) =>{console.log (Doc1)//{id : 2,name: "BB", age:33} console.log (DOC2)//{id:3,name: "CC", Age:3}}) New Model (DOC). Save () Eg.new Usermodel ({id:1,name: ' AA ', age:23}). Save () Delete the document Model.remove (conditions, (Err,doc) =>{} on the model and delete the document eg. Model.find ({id:{$lte: 5}, (Err,docs) =>{docs.remove ()}) Change Model.update (Conditions,update, (Err,raw) =>{}) Check Model.find (conditions, [fields], [options], [(Err,docs) =>{}]) Parameters: Query criteria, Control returned fields (field filter), configure query parameters, The callback function conditions query condition reference MONGO syntax, such as fields such as $lte:32, is the field that controls the return data, that is, which key you want to write ' name ' to return to name, and the default is to return the _id value, written as {name:1,_ id:0} options can be configured query parameters, such as paging when {limit:20} means that return 20 data (insufficient return all) callback in Err means a query error, and docs represents the returned result. FindOne () represents the first of the returned data.     FindByID () indicates that the query is based on obj.id to find the sortSort skip skip Limit limits select Show field exec perform ID descending, age ascending sort model.find (). Sort ('-id age '). EXEC ((err,docs) =>{}) Skip the first three data mode L.find (). Skip (3). EXEC ((err,docs) =>{}) displays only 13 data Model.find (). (). exec ((err,docs) =>{}) displays only the name age field, Do not display Idmodel.find (). Select (' Name Age-id '). EXEC ((err,docs) =>{}) above can be connected to write, find data by ID descending age ascending, skip Top 3, show only 13, Each data displays only the Name,age field and does not display Idmodel.find (). Sort ('-id age '). Skip (3). Limit ("Name Age-id"). Exec ((err,docs) = {Console.log (Docs)}) Paging Small Example: Define page numbers, define how many pages per page, define let page = Req.param (' page ') let pageSize = 20let skip = (page-1) *pagesizemodel.find (). Skip (Skip).        Limit (pageSize). EXEC ((Err,docs) =>{if (err) {Data.push ({status = 1, msg: ' Data error ') })}else{Data.push (DOC)}})

  

Mongoose Packet Operation MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.