MongoDB database operations 2

Database operations mongoose 2

1 Introduction

  • monogoose module (master)
  • Examples of needs analysis (understanding)
  • Environmental structures (master)
  • MongoDB CURD operations (focus)

2.mongoose module (master)

2.1.Mongoose Profile

Mongoose library is short of operating a MongoDB database packaged in convenient node environment, an object-modeling tools, Mongoose convert the data in the database as a JavaScript object for you to use in your application.

The official document: https://www.npmjs.com/package/mongoose
http://mongoosejs.com/

2.2.Mongoose operating procedures (focus of the focus)

2.2.1. Installation mongoose

npm install mongoose

2.2.2 database connection

Use mongoose connection to the database provided that:

  1. mongo database is already running.

  2. The mongoose package has been installed

        // 连接字符串格式为mongodb://主机/数据库名
        mongoose.connect('mongodb://localhost/student);
    上面这句的意思是连接到本地的mongodb的student表。
    
    

Extended:

// 连接本地mongodb ,本机的ip 127.0.0.1,端口:27017 数据库:student
mongoose.connect("mongodb://127.0.0.1:27017/student",function(err){
   if(!err){//如果连接成功,则打印出connected to Mongodb
      console.log("connected to Mongodb");
   }else{
      throw err;//如果连接失败,则抛出异常
   }
});
2.2.3. New data

First explain a concept Schema:

It can be understood as a database model skeleton, can be regarded as the plant dies, like a cup, drink plenty of water is the ultimate function of the cup, the cup itself is like a Model, then the cup is the need to rely on mass production factory of mold forming. Schema defines not only the document structure and performance, you can have extensions, instance methods, static methods, a composite index, document lifecycle hook.

Schema: a kind of database files stored in the form of a skeleton model, does not have the ability to operate the database
Model: publishing model generated by the Schema, with database operator abstract properties and behavior of the
Entity: entity created by the Model, his operation will affect the database

Data storage: defining Schema (backbone)> Create model (model)> Examples of the method of the Entity.

//通过Schema创建一个模式NewsSchema  
var studentSchema  = new Schema({      
     name: String,
     age: String
});

studentSchema  类型有哪些:

//通过模式studentSchema  创建一个模型studentModel 
var studentModel = mongoose.model("students",studentSchema  );

var instance1 = new studentModel (); 
instance1.name="tangyan";
instance1.age="18";
//通过save方法保存
instance1.save(function(err){
    if (err) {
       console.log('保存失败');  
       return;
    }
 });
2.2.4. Deleting Data

Ideas:

1, delete data must first know which one to delete the information you need to know id information.
2, the data corresponding to the id check out, perform the remove method
if we want to delete the data id = "57e24521a755e1154039a403" of.

var id=”57e24521a755e1154039a403”; 
studentQueryModel.findById(id,function(err,doc){
   if(!doc){
      return next(new NotFound("Doc not found"))
   }else{
      doc.remove(function(){
         console.log('删除成功');
      })
   }
});
2.2.5. Modify data

Modify the data of ideas: the first corresponding to the id query data out of the need to modify the field to re-assignment, and then perform the Save method to save.
If we want to modify the data id = "57e24521a755e1154039a403" of.

var id=”57e24521a755e1154039a403”; 

studentQueryModel.findById(id,function(err,doc){
   //console.log(doc);
   doc.name="xiaotangyan" ;//把name修改为xiaotangyan
   doc.save(function(err){
      if(!err){
          console.log('修改成功');
      }else{
         throw err;
      }
   });
});
2.2.6 query data

Here find {} The first parameter which is empty, all the data are queried:
docs shows the results of a query set, may be used to render ejs template engine.

studentQueryModel.find({},function(err,docs){
	console.log(docs);
});

In another case, see PC

Guess you like

Origin blog.csdn.net/weixin_45213847/article/details/93306918