Mongoose Learning Notes-basic knowledge 1

Source: Internet
Author: User

Today we will learn mongoose, what is mongoose, it is in MongoDB what is the relationship, it can be used to do?

MongoDB is an open source NoSQL database, compared to MySQL-like relational database, it is more light and flexible, it is very suitable for use in the case of large data size, not strong transaction. At the same time, it is also an object database, no tables, rows and other concepts, there is no fixed pattern and structure, all the data stored in the form of documents (document, is an associative array of objects, its interior consists of attributes, a property corresponding to the value may be a number, string, date, array, or even a nested document.) ), the data format is JSON.

The introduction of MongoDB, we need to know mongoose.

1. What is Mongoose?

Mongoose is an object model tool for MongoDB and is a node-mongodb-native-developed MongoDB Nodejs driver that can be executed in an asynchronous environment. At the same time, it is also an object model library for MongoDB operation, which encapsulates some commonly used methods such as MongoDB's addition and deletion to document, so that Nodejs operation MongoDB database becomes more flexible and simple.

2. What can mongoose do?

Mongoose, because it encapsulates the usual processing method for MongoDB to document operation, let Nodejs operation MongoDB database become easy, easier, so easy!

Learn the above introduction, I believe you have a preliminary understanding and understanding of the mongoose, a beginning, a step, run it, juvenile!

MongoDB is an object database that is used to store data;

Mongoose is an object model library that encapsulates the MongoDB operation and is used to manipulate the data.

OK, let's take the first step in manipulating the data.

1. Install Mongoose:

NPM Install Mongoose

2. Reference Mongoose:

var mongoose = require ("Mongoose");

3. Connect to the database using "Mongoose":

var db = Mongoose.connect ("Mongodb://user:[email protected]:p ort/database");

Note:var db = Mongoose. Connect("mongodb://127.0.0.1:27017/test");//Such a form, We want to link to the default port on the MongoDB database 127.0.0.1:27017, test is the collection name;

4. Execute the following code to check the default database test, can the normal connection succeed?

Eg: Practice

Open the database, create App.js, and enter the following code;

Run the node app in node;

var mongoose = require ("Mongoose"), var db = Mongoose.connect ("mongodb://127.0.0.1:27017/test");d B.connection.on (" "Error", function (Error) {console.log ("Database connection failed:" + error);}); Db.connection.on ("Open", function () {Console.log ("------Database connection succeeded! ------");});

  

Understanding Collections

First, let's briefly explain the MongoDB database again.

mongodb--is an object database, without the concept of tables, rows, or fixed patterns and structures, all data is stored in document (documents), which is an associative array of objects, which are internally composed of attributes, The value corresponding to an attribute may be a number, string, date, array, or even a nested document. ), we'll learn how to create a document and insert content later.

In MongoDB, multiple document can be composed of collection (hereinafter referred to as a collection), and multiple collections can form a database.

If we want to manipulate MONGODB data, we have to have the above mentioned "document" containing the data, what is the meaning of the document, please see the following introduction.

Document--is the core concept of MongoDB, is an ordered set of key-value pairs, in JavaScript, the document is represented as an object. It is also the basic unit of data in MongoDB, very similar to the row in relational database management system, but more expressive.

Collection-a set of documents that, if a document in MongoDB is likened to a row in a relational database, is the equivalent of a single table.

If we are going to create a "collection" through mongoose, and how to do it, how to do it, we must first understand the schema (data property model), model, entity!

Mongoose Learning Notes-basic knowledge 1

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.