摘要:對象產(chǎn)生了,同時也持久化數(shù)據(jù)進(jìn)入數(shù)據(jù)庫了。初步使用連接數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)庫連接監(jiān)聽事件數(shù)據(jù)庫成功連接定義模型創(chuàng)造定義在上的方法創(chuàng)造模型參數(shù)是,定義字段。創(chuàng)建模型創(chuàng)建一個結(jié)構(gòu)。不但創(chuàng)建的是貓的實(shí)例,查詢出來的也是貓的實(shí)例。
Mongoose簡介
是一個將JavaScript對象與數(shù)據(jù)庫產(chǎn)生關(guān)系的一個框架,Object related model。操作對象,就是操作數(shù)據(jù)庫了。對象產(chǎn)生了,同時也持久化(數(shù)據(jù)進(jìn)入數(shù)據(jù)庫)了。
初步使用Mongoose 連接數(shù)據(jù)庫var mongoose = require("mongoose"); //創(chuàng)建數(shù)據(jù)庫連接 var db = mongoose.createConnection("mongodb://localhost:27017/zf"); //監(jiān)聽open事件 db.once("open",function ( callback ) { console.log("數(shù)據(jù)庫成功連接"); }); module.exports = db;定義模型
創(chuàng)造schema -> 定義在schema上的scatic方法 -> 創(chuàng)造模型
new mongoose.schema({}); //參數(shù)是json,定義字段。
創(chuàng)建模型 db.model(collectionsName,schemaName);
var mongoose = require("mongoose"); var db = require("./db.js"); //創(chuàng)建一個schema結(jié)構(gòu)。 schema--模式 var StudentSchema = new mongoose.Schema({ name: {type: String, default: "匿名用戶"}, age: { type: Number }, sex: { type: String } }); // 創(chuàng)建方法 StudentSchema.statics.zhaoren = function ( name,callback ) { this.model("Student").find({"name": name},callback); } //創(chuàng)建修改方法 StudentSchema.statics.xiugai = function ( conditions,update,options,callback ) { this.model("Student").update(conditions,update,options,callback); } var studentModel = db.model("Student",StudentSchema); module.exports = studentModel;
app.js 中只操作類,不操作數(shù)據(jù)庫。
var Cat = mongoose.model("Cat"{"name": String, age: Number}); Cat.find({"name": "tom"},function( err.reslut ){ var xiaomao = reslut[0]; //小貓這個變量是一個Cat的實(shí)例,它是從Cat集合中find出來的,所以find出來以后,就是Cat的一個實(shí)例。 //不但創(chuàng)建的是貓的實(shí)例, find查詢出來的也是貓的實(shí)例。 xiaomao.age = 10; xiaomao.save(); })Schema
定義文檔結(jié)構(gòu)支持的類型
String Number Date Buffer Boolean Mixed ObjectId Array定義對象(methods)方法
實(shí)例出來的對象,使用的方法, 實(shí)例來調(diào)用。
var mongoose = require("mongoose"); mongoose.connect("mongodb://localhost/mlln"); var db = mongoose.connection; db.on("open",function ( callback ) { console.log("數(shù)據(jù)庫成功打開"); }); var animalSchema = new mongoose.Schema({ "name": String, "type": String }); animalSchema.methods.zhaotonglei = function ( cb ) { this.model("Animal").find({"type": this.type},cb); } var Animal = mongoose.model("Animal",animalSchema); //module.exports = Blog; /*Animal.create({"name": "湯姆","type": "貓"}); Animal.create({"name": "imim","type": "貓"}); Animal.create({"name": "小白","type": "狗"}); Animal.create({"name": "加菲貓","type": "貓"}); Animal.create({"name": "snoopy","type": "狗"}); */ //blog.save(); Animal.findOne({"name": "imim"},function ( err,reslut ) { var dog = reslut; dog.zhaotonglei(function ( err,resluts ) { console.log( resluts ); }); });model文檔操作 構(gòu)造函數(shù)
構(gòu)造函數(shù), 參數(shù)1:集合名稱, 參數(shù)2:Schema實(shí)例
db.model(“test1”, TestSchema );查詢
查詢, 參數(shù)1忽略,或?yàn)榭諏ο髣t返回所有集合文檔
model.find({}, callback); model.find({},field,callback); //過濾查詢,參數(shù)2: {‘name’:1, ‘a(chǎn)ge’:0} 查詢文檔的返回結(jié)果包含name , 不包含age.(_id默認(rèn)是1) model.find({},null,{limit:20}); //過濾查詢,參數(shù)3: 游標(biāo)操作 limit限制返回結(jié)果數(shù)量為20個,如不足20個則返回所有. model.findOne({}, callback); //查詢找到的第一個文檔 model.findById(‘obj._id’, callback); //查詢找到的第一個文檔,同上. 但是只接受 __id 的值查詢創(chuàng)建
創(chuàng)建, 在集合中創(chuàng)建一個文檔
Model.create(文檔數(shù)據(jù), callback))更新
更新,參數(shù)1: 查詢條件, 參數(shù)2: 更新對象,可以使用MondoDB的更新修改器
Model.update(conditions, update, function(error)刪除
刪除, 參數(shù)1: 查詢條件
Model.remove(conditions,callback);
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/86364.html
閱讀 3561·2021-11-19 09:40
閱讀 1573·2021-10-13 09:41
閱讀 2784·2021-09-29 09:35
閱讀 2779·2021-09-23 11:21
閱讀 1812·2021-09-09 11:56
閱讀 922·2019-08-30 15:53
閱讀 901·2019-08-30 15:52
閱讀 660·2019-08-30 12:47