mongoose– enables node. js to operate MongoDB efficiently (reproduced)

Source: Internet
Author: User
Tags mongodb relationships mongodb server string format

Mongoose Library in short, a convenient package for manipulating MongoDB databases in the node environment, an object model tool similar to Orm,mongoose converts data from a database into JavaScript objects for you to use in your app.

Of course, you have to install the Environment node. js and MongoDB = "MongoDB installation

MongoDB is one of the most popular NoSQL databases, developed specifically for node. js, and can be used as a basic understanding of the differences between the RDBMS relational databases that we commonly use.

There are three basic concepts in MongoDB that are different from RDBMS:

    1. Database: A database is a collection of physical containers. Each database has its own set of files on the file system. A single MongoDB server typically has multiple databases.
    2. Collection: A collection is a set of MongoDB documents. It is equivalent to an RDBMS table. The collection exists in a single database. The collection does not execute the pattern. The documents within the collection can have different areas. Typically, all files in a collection are of the same or related purpose
    3. Document: A document is a set of key-value pairs. File dynamic mode. Dynamic mode refers to documents in the same collection that do not require a collection of common fields that have the same field or structure group, and can accommodate different types of data.

The table given below shows RDBMS terminology using MongoDB relationships

  

The most important thing to observe is that the tables and fields in our relational database are replaced with the concept of the document, the MONGO is not in the relationship with the primary key, and the data is stored in the document style with JSON style key value.

Learn more about the topics that can be asynchronous MongoDB data summarization

Operating Procedures in the Mongoose

1. Connect to the database

var mongoose = require ("Mongoose"); // the connection string format is MongoDB: // Host/Database name mongoose.connect ('mongodb://localhost/test');

There is not much to explain here, after installing mongoose, the introduction of modules connected to the local MongoDB test table, here to note that the MONGO database is running state

  2. Storing data

varSchema =Mongoose. Schema;//Skeleton TemplatesvarMovieschema =NewSchema ({doctor:string, title:string, language:string, country:string, Year:number , Summary:string, poster:string, flash:string})//ModelvarMovie = Mongoose.model ('Movie', Movieschema);//Storing DatavarMoive =NewMovie ({title:'three people in black clothes', Doctor:'Smith', Year:2018, Flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf', Country:'United States', Language:'English', Summary:'Good film'})//Save DatabaseMoive.save (function (err) {if(Err) {Console.log ('Save failed')        return; } console.log ('Meow');});

The process of schema can be omitted directly.

/*var Schema = Mongoose.  schema;//skeleton template var movieschema = new Schema ({doctor:string, title:string, language:string, country : String, Year:number, summary:string, poster:string, flash:string})*///establishing a relational model directly through modelvarMovie = Mongoose.model ('Movie', {doctor:string, title:string, language:string, Country:string, Year:number, S Ummary:string, poster:string, flash:string});//Storing DatavarMoive =NewMovie ({title:'three people in black clothes', Doctor:'Smith', Year:2018, Flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf', Country:'United States', Language:'English', Summary:'Good film'})//Save DatabaseMoive.save (function (err) {if(Err) {Console.log ('Save failed')        return; } console.log ('Meow');});   

These are the simplest data operations.

The process is to create a schema through the schema Movieschema, create a model movie from the schema Movieschema, write the data through the model movie, save by the Save method

Define Schema-> Create Model-> instantiation method

Tables – "Collections –" Documentation

Here are a few noun concept schemas andModel

  3.Schema与Model

    • Schema: A database model skeleton stored as a file, not capable of database operation
    • Model: Generated by schema Publishing, database operations with abstract properties and behavior

Although schemas are not required in mongodb storage, generally we use patterns in mongoose for the sake of consistent documentation. It can be said that everything in mongoose from the definition of the mode

Unlike traditional relational databases, such as MySQL, where data is connected directly to a given method, the SQL statement is executed, and there is an abstraction of the schema

Schema it is similar to the table structure of a relational database and can be understood as a database model skeleton

The schema can be seen as a mold in the factory, like a teacup, drink water is the ultimate function of the cup, the Cup itself is like model, then the cup production is to rely on the factory mold, which is like a schema

Schema not only defines document structure and usage performance, but also extensions, instance methods, static methods, composite indexes, document life cycle hooks

4. Curd operation

Model can see the table in the relational database, then the instance document through the new model corresponds to a row of tables in the relational database

Document has many built-in instance methods. Can directly perform additions and deletions to check the operation

  Add data

//ModelvarMovie = Mongoose.model ('Movie', Movieschema);
Storing DatavarMoive =NewMovie ({title:'three people in black clothes', Doctor:'Smith', Year:2018, Flash:'http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf', Country:'United States', Language:'English', Summary:'Good film'})//Save DatabaseMoive.save (function (err) {if(Err) {Console.log ('Save failed') return; } console.log ('Meow');});

Here we generate a document called Moive by instantiating the model movie and storing it in a collection using the Save method

  modifying data

Storing data
var moive = new Movie ({});
Update data
moive.update ({_id:oneuser._id}, {    $set: {Name:oneuser.name,password:oneuser.password}} , function (err) {    if(err) {        console.log (err)        return    }    Console.log (' update success ')});

  Delete data

User.remove ({    _id:id}, function (err) {    if  (err        ) {console.log (err)  return    }    console.log (' Delete succeeded ')});

Simple database connection, run through the entire process, the specific can view the API

Mongoose api:http://mongoosejs.com/docs/api.html

Attach a simple test case for git on mongoose

Https://github.com/JsAaron/node_movie/tree/master/data

Support for adding, modifying, and deleting data

  

Win Platform Installation:

1. First install the MongoDB installation

2. Enter the catalogue and install module dependencies, Grunt, Mongoose, Express, Jade via NPM installation

3. Execute command grunt, automatically detect file change restart service

4. Open Http://localhost:3000/

Information:

Mongoose– makes Nodejs easier to manipulate MongoDB database: http://floss.zoomquiet.io/data/20110718215356/index.html

http://hao.jser.com/archive/7767/

Express+mongoose:http://qianduanblog.com/post/nodejs-learning-7-express-mongoose.html

nodejs:http://qianduanblog.com/category/nodejs/

mongoose– enables node. js to operate MongoDB efficiently (reproduced)

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.