Mongoose Learning Documents

Source: Internet
Author: User

noun explanation
    • Schema: A database model skeleton stored as a file, not capable of database operation

    • Model: The Schema model generated by the publication, database operations with abstract properties and behaviors

From the Cnode community

1. Create a local database and connect:

var mongoose = require (' Mongoose ');     Get Mongoose Module Mongoose.connect (' Mongodb://localhost/nodejs '); Connect to the database using the Connect method of the Mongoose object Nodejs

2. Define the database document type:

For mongoose, all actions are derived from the Schema (model), so the following is the declaration and definition of the schema:

var Schema = Mongoose. Schema;var Movieschema = new Schema ({    name:string,    alias: [String],    publish:date,    create_date:{type : Date,default:date.now},    image:{        coversmall:string,        coverbig:string    },    source:[{        Source:string,        link:string,        swflink:string,        quality:string,        version:string,        Lang: String,        subtitle:string,        Create_date:{type:date,default:date.now}    ]});

Variable names can be arbitrarily taken, here the Mongoose attribute schema is assigned to the schema variable, and then a new object through the schema, which determines the shape of the document in the database (format)

3. Publish the schema as model:

var movie = Mongoose.model ("movie", Movieschema), var Moviedao = function () {};module.exports = new Moviedao ();

Define a variable to receive the release of the model, the parameters of the movie is the database inside the collection (for example, the above database for Nodejs, where the movie is Nodejs inside the collection), but it seems mongoose not case-sensitive, here is the movie, The view database is movie.

The second line of code defines an empty object, and the third line of code leads to an instance of the empty object, and the next step is to add a method to the empty object.

4. Adding, changing and checking the model

1) New

To add a new document, we must first instantiate a document (above the movie):

MovieDAO.prototype.save = function (obj, callback) {    var instance = new Movie (obj);    Instance.save (function (err) {        callback (ERR);    });

The Save method passes in an Obj object, which is used to instantiate the movie, and the instantiated object is assigned to instance, so that instance is a real document and all the methods of the movie, and then you can use Save (the official API, We just need to care about the callback function to insert the document.

2) query

MovieDAO.prototype.findByName = function (name,callback) {    Movie.findone ({name:name},function (err,obj) {        Callback (err,obj);    };

This is queried in the Name field.

3) Update

MovieDAO.prototype.updateData = function (name,set,callback) {    movie.update ({name:name},set,function (err) {        callback (ERR);    });};

Here the Name field is used as the query condition, set as the update data, such as {name: "Shenzhen"}

Mongoose Learning Documents

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.