node. js efficiently manipulating MongoDB

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

node. js efficiently manipulating MongoDB

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 understands the difference between our common RDBMS relational database and the basic use of

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

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}) Model var movie = Mongoose.model (' movie ', Movieschema);//Storage data var moive = new Movie ({    title: ' Black man Three ',    Doctor: ' Smith ',    year:2018,    Flash: ' http://player.youku.com/player.php/sid/XNjA1Njc0NTUy/v.swf ',    Country: ' America ',    language: ' English ',    Summary: ' Good piece '})//Save Database Moive.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

Model
var moive = new Movie ({    title: ' Black man Three ',    Doctor: ' Smith ',    year:2018,    Flash: ' http://player.youku.com/ Player.php/sid/xnja1njc0ntuy/v.swf ',    Country: ' Us ',    language: ' English ',    Summary: ' Good piece '} '//Save Database Moive.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

User.update ({_id:oneuser._id}, {    $set: {Name:oneuser.name,password:oneuser.password}}, function (err) {    if (err) {        console.log (err)        return    }    console.log (' Update succeeded ')});

Updated data less with $set, usability is good

Delete data

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

node. js efficiently manipulating 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.