Mongoose Learning Notes-basic knowledge 2

Source: Internet
Author: User

Schema Brief

schema--is a kind of database model skeleton stored in file form, which can not directly go to the database side, that is, it does not have the ability to operate the database, just a representation of the database model in the program fragment, it is a data attribute model (the traditional meaning table structure), or a "set" model skeleton.

So how to define a schema, see example:

var mongoose = require ("Mongoose"); var testschema = new Mongoose. Schema ({name: {type:string},//property name, type String age    : {type:number, default:0},//attribute age, type number, default 0 time    : {type:date, Default:Date.now},email: {type:string,default: '}});

Note: The basic attribute types are: string, date, numeric, Boolean (Boolean), NULL, array, inline document, and so on.

Model Brief

model--the model generated by schema constructs, in addition to the database skeleton defined by the schema, has the behavior of database operations, similar to the classes that manage database properties and behaviors.

How to create a model from the schema, as shown in the following example:

var db = Mongoose.connect ("Mongodb://127.0.0.1:27017/test"); Create Modelvar Testmodel = Db.model ("Test1", Testschema);

  

Test1: The collection name in the database, when we add data to it, if test1 already exists, it is saved to its directory, and if it does not exist, the Test1 collection is created, and then the data is saved.

Has the model, we also have the operating database of the Golden Key, in the later course, we will learn to use model to do the specific operation of the increase and deletion check, so, must be familiar with his creation format Yo!

If you want to make a difference to a collection, give it to the model and create a model that we need to specify: 1. Collection name, 2. The schema structure object of the collection, which satisfies both conditions, we will have a golden key to operate the database.

Entity Brief

entity--entities created by the model use the Save method to save data, both model and entity have operations that can affect the database, but the model is more operational than entity.

Create an entity using model, as in the following example:

var testentity = new Testmodel ({name: "Lenka", Age:36,email: "[Email protected]"}); Console.log (Testentity.name); Lenkaconsole.log (Testentity.age); 36

Note: After a successful creation, the schema property becomes the public property of model and entity.

Create a Collection

Based on the previous course, we will begin to learn the specific operation of the data, the following is about some basic data definition, I believe that you are no stranger, please review it again!

var mongoose = require ("Mongoose"); var db = Mongoose.connect ("mongodb://127.0.0.1:27017/test"); var testschema = new Mongoose. Schema ({name: {type:string},age: {type:number, default:0},email: {type:string},time: {type:date, default:Date.no W}}), var Testmodel = Db.model ("Test1", Testschema), var testentity = new Testmodel ({name: "HelloWorld", Age:28,email: " [email protected]}); Testentity.save (function (Error,doc) {if (error) {Console.log ("Error:" + error);} Else{console.log (DOC);}});

Note: In order to facilitate the learning of later courses and improve the efficiency of learning, the following basic data are closely related to the following courses, so it must be defined as follows structure, do not modify (default database is test, set to Test1)

Summarize:

1. Schema: A model skeleton of a database collection, or a table structure that is traditional to the Data property model.

2. Model: Constructed from a schema, in addition to a schema-defined database skeleton, you can also specifically manipulate the database.

3. Entity: Entities created through model, it can also manipulate the database.

Mongoose Learning Notes-basic knowledge 2

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.