node21---mongoose
Posted 672530440
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node21---mongoose相关的知识,希望对你有一定的参考价值。
01.js
//引包 var mongoose = require(‘mongoose‘); //创建一个数据库连接 mongoose.connect(‘mongodb://localhost/test‘); //创建一个Cat类。 语法mongosse.model(模型名字,Schema); //这里省略了一步,就是schema是通过new mongoose.schema({})创建的(类工厂)。 var cc = mongoose.model(‘Cat‘, { name: String , age : Number , sex : String }); //对象 var kitty = new cc({ name: "汤姆" , "sex" : "公猫"}); //var kitty1 = new cc({ a: "汤姆"}); 是不行的,因为schema定了。 kitty.save(function (err) { console.log(‘meow‘); }); //寻找汤姆猫,将它改为8岁。 cc.find({"name":"汤姆"},function(err,result){ console.log(result);//一条记录 var xiaomao = result[0]; //xiaomao这个变量是一个Cat的实例 xiaomao.age = 8; xiaomao.save(); });
02.js
/** * mongoose官网中,Guide是学习路线,API Doc是api接口。 索引不能重复,查找快 */ var mongoose = require(‘mongoose‘); mongoose.connect(‘mongodb://localhost/test‘); var db = mongoose.connection; db.once(‘open‘, function (callback) { console.log("数据库成功打开"); }); //博客的结构(Schema是类的模版或者是一个创建类的工厂,他创建的类有这些属性) var blogSchema = new mongoose.Schema({ title: String, author: String, body: String, comments: [{ body: String, date: Date }], date: { type: Date, default: Date.now }, hidden: Boolean, meta: { votes: Number, favs: Number } }); //这个工厂创建的类还有这个方法(对象也有) blogSchema.methods.showInfo = function(){ console.log(this.title); } //通过工厂创建一个有名类,类的名字是Blog,也可以是别的名字。 var bb = mongoose.model(‘Blog‘, blogSchema); var blog = new bb({//Blog类的对象 "title" : "博客测试", "author" : "考拉" }); //blog.save(); blog.showInfo();
03.js
mongoose.connect(‘mongodb://localhost/test‘); var db = mongoose.connection; db.once(‘open‘, function (callback) { console.log("数据库成功打开"); }); //博客的结构(类工厂,工厂创建的类有这些属性) var animalSchema = new mongoose.Schema({ "name" : String, "type" : String }); //工厂创建的类有这些方法(对象也有) animalSchema.methods.zhaotonglei = function(callback){ this.model(‘Animal‘).find({"type":this.type},callback); } //通过schema工厂创建一个有名的Animal类 var Animal = mongoose.model(‘Animal‘, animalSchema); //creat是类的方法,创建对象。 Animal.create({"name":"汤姆","type":"猫"}); Animal.create({"name":"咪咪","type":"猫"}); Animal.create({"name":"小白","type":"狗"}); Animal.create({"name":"snoopy","type":"狗"}); Animal.findOne({"name":"小白"},function(err,result){ var dog = result;//result是Animal的实例 dog.zhaotonglei(function(err,result){ console.log(result); }); });
04.js
/** * Created by Danny on 2015/9/29 10:18. */ var mongoose = require(‘mongoose‘); mongoose.connect(‘mongodb://localhost/test‘); var db = mongoose.connection; db.once(‘open‘, function (callback) { console.log("数据库成功打开"); }); //博客的结构 var blogSchema = new mongoose.Schema({ title: String, author: String, body: String, comments: [{ body: String, date: Date }] }); //发表评论 blogSchema.methods.pinglun = function(obj,callback){ this.comments.push(obj); this.save(); } var Blog = mongoose.model(‘Blog‘, blogSchema); //var blog = new Blog({ // "title" : "哈哈哈", // "author" : "考拉", // "body" : "哈哈哈哈" //}); //寻找一个标题是哈哈哈的博客,然后发表评论 Blog.findOne({"title":"哈哈哈"},function(err,blog){ if(!blog){ return; } blog.pinglun({"body":"再来一个评论","date" : new Date()}); });
05.js
/** * Created by Danny on 2015/9/29 10:18. */ var mongoose = require(‘mongoose‘); mongoose.connect(‘mongodb://localhost/xuanke‘); var db = mongoose.connection; db.once(‘open‘, function (callback) { console.log("数据库成功打开"); }); //学生工厂 var studentSchema = new mongoose.Schema({ "name" : String, "age" : Number, "sex" : String }); //实例方法,涨一岁 studentSchema.methods.zhangyisui = function(){ this.age++; this.save();//改变数据库,要save() } //学生类 var Student = mongoose.model("Student",studentSchema); //课程工厂 var kechengSchema = new mongoose.Schema({ "name" : String, "info" : String, "student" : [studentSchema]//课程表里面会有学生的全部json,不是外键 }); //添加学生 kechengSchema.methods.tianjiaxuesheng = function(studentObj,callback){ this.student.push(studentObj); this.save(function(){ callback(); }); } kechengSchema.methods.zhaoxuesheng = function(num,callback){ //要通过学生类去找,this是课程对象, Student.findOne({"name":this.student[num].name},function(err,result){ callback(err,result); }); } //课程类 var Kecheng = mongoose.model("Kecheng",kechengSchema); //实例化几个学生 var xiaoming = new Student({"name":"小明","age":12,"sex":"男"}); xiaoming.save(); Student.creat({"name":"小hua","age":10,"sex":"nv"}); var shuxue = new Kecheng({ "name" : "数学课", "info" : "学数学的" }); shuxue.tianjiaxuesheng(xiaoming,function(){ console.log("添加成功"); }); //寻找学生小明 Student.findOne({"name":"小明"},function(err,student){ student.zhangyisui();//学生表改了,课程表没改, }); //通过课程找学生 Kecheng.findOne({"name":"数学课"},function(err,kecheng){ kecheng.zhaoxuesheng(0,function(err,result){ result.zhangyisui(); console.log(result); }); });
以上是关于node21---mongoose的主要内容,如果未能解决你的问题,请参考以下文章