【mongo】mongoose

mongoose

api:hhttp://mongoosejs.com/docs/ap...html

安裝及引用

安裝

npm install mongoosejava

引用mongoose

var mongoose = require(「mongoose」);正則表達式

使用mongoose連接數據庫

api

mongoose.connect('mongodb://username:password@host:port/database?options...');mongodb

test

mongoose.connect('mongodb://localhost/myapp');數據庫

示例

var mongoose = require(「mongoose」); 
var db = mongoose.connect(「mongodb://127.0.0.1:27017/test」); 
db.connection.on(「error」, function (error) { 
console.log(「數據庫鏈接失敗:」 + error); 
}); 
db.connection.on(「open」, function () { 
console.log(「——數據庫鏈接成功!——」); 
});

MongoDB基礎

Schema : 一種以文件形式存儲的數據庫模型骨架,不具有數據庫的操做能力npm

Model : 由Schema發佈生成的模型,具備抽象屬性和行爲的數據庫操做對api

Entity : 由Model建立的實體,他的操做也會影響數據庫數組

Schema

一種以文件形式存儲的數據庫模型骨架,沒法直接通往數據庫端,也就是說它不具有對數據庫的操做能力.能夠說是數據屬性模型(傳統意義的表結構),又或着是「集合」的模型骨架promise

/* 定義一個 Schema */
var mongoose = require("mongoose");

var TestSchema = new mongoose.Schema({
    name : { type:String },//屬性name,類型爲String
    age  : { type:Number, default:0 },//屬性age,類型爲Number,默認爲0
    time : { type:Date, default:Date.now },
    email: { type:String,default:''}
});

上面這個 TestSchema包含4個屬性 [name, age, time, email]app

基本屬性類型有:字符串、日期型、數值型、布爾型(Boolean)、null、數組、內嵌文檔等

api

Schema.prototype.clone()

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://127.0.0.1:27017/test'); 
var mongooseSchema = new mongoose.Schema({
  username : {type : String, default : '匿名用戶'},
  title    : {type : String},
  content  : {type : String},
  time     : {type : Date, default: Date.now},
  age      : {type : Number}
});
var cloneSchema = mongooseSchema.clone();

Schema.prototype.add()

var ToySchema = new mongoose.Schema;
ToySchema.add({ name: 'string', color: 'string', price: 'number' });

Schema.prototype.path()

schema.path('name') // returns a SchemaType
schema.path('name', Number) // changes the schemaType of `name` to Number
var mongooseSchema = new mongoose.Schema({
  username : {type : String, default : '匿名用戶'},
  title    : {type : String},
  content  : {type : String},
  time     : {type : Date, default: Date.now},
  age      : {type : Number}
});
console.log(mongooseSchema.path('time'));
輸出:
SchemaDate {
  path: 'time',
  instance: 'Date',
  validators: [],
  setters: [],
  getters: [],
  options: { type: [Function: Date], default: [Function: now] },
  _index: null,
  defaultValue: [Function: now] }

中間件api

參考文檔:https://www.cnblogs.com/surah...

Schema.prototype.pre()

有2種類型的pre hook,串行(seria)和並行(parallel)。

Serial

串行中間件是一個接一個執行,每一箇中間件調用next。

var schema = new Schema(..);
schema.pre('save', function(next) {
  // 作些什麼
  next();
});
Parallel

並行中間件提供更細粒度的操做

var schema = new Schema(..);

// 'true'表示這是一個並行中間件. 若是你想要使用並行中間件,你必須指定true做爲第二個參數 
schema.pre('save', true, function(next, done) {
  // 下一個要執行的中間件並行執行
  next();
  setTimeout(done, 100);
});

Schema.prototype.post()

Post 中間件

post中間件在hooked方法和全部它的pre中間件完成後才執行。post中間件不直接接收流控制,如沒有next和done回調都傳遞給它。post hook可以爲這些方法註冊傳統的事件監聽器。

schema.post('init', function(doc) {
  console.log('%s has been initialized from the db', doc._id);
});
schema.post('validate', function(doc) {
  console.log('%s has been validated (but not saved yet)', doc._id);
});
schema.post('save', function(doc) {
  console.log('%s has been saved', doc._id);
});
schema.post('remove', function(doc) {
  console.log('%s has been removed', doc._id);
});

Schema.prototype.method()

追加方法
Schema.methods.say = function(){console.log(‘hello’);};
//靜態方法,只限於在Model層就能使用

實例

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://127.0.0.1:27017/test'); 
var mongooseSchema = new mongoose.Schema({
  username : {type : String, default : '匿名用戶'},
  title    : {type : String},
  content  : {type : String},
  time     : {type : Date, default: Date.now},
  age      : {type : Number}
});
// 添加 mongoose 實例方法
mongooseSchema.methods.findbyusername = function(username, callback) {
  return this.model('mongoose').find({username: username}, callback);
}
var mongooseModel = db.model('mongoose', mongooseSchema);
var mongooseEntity = new mongooseModel();
mongooseEntity.findbyusername('model_demo_username', function(error, result){
          if(error) {
              console.log(error);
          } else {
              console.log(result);
          }
          //關閉數據庫連接
          db.close();
      });
公共方法

這樣Model和Entity的實例就能使用這個方法了

mongooseSchema.method('meow', function () {
  console.log('meeeeeoooooooooooow');
})

var Kitty = db.model('Kitty', mongooseSchema);

var fizz = new Kitty;
fizz.meow();
schema.method({
    purr: function () {}
  , scratch: function () {}
});

// later
fizz.purr();
fizz.scratch();

Schema.prototype.static()

靜態方法,只限於在Model層就能使用

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://127.0.0.1:27017/test'); 
var mongooseSchema = new mongoose.Schema({
  username : {type : String, default : '匿名用戶'},
  title    : {type : String},
  content  : {type : String},
  time     : {type : Date, default: Date.now},
  age      : {type : Number}
});
// 添加 mongoose 靜態方法,靜態方法在Model層就能使用
mongooseSchema.statics.findbytitle = function(title, callback) {
  return this.model('mongoose').find({title: title}, callback);
}
var mongooseModel = db.model('mongoose', mongooseSchema);

mongooseModel.findbytitle('emtity_demo_title',function(err,res){})

model

參考文檔:http://www.cnblogs.com/surahe...
由Schema構造生成的模型,除了Schema定義的數據庫骨架之外,還具備數據庫操做的行爲,相似於管理數據庫屬性、行爲的類

var db = mongoose.connect("mongodb://127.0.0.1:27017/test");

// 建立Model
var TestModel = db.model("test1", TestSchema);
test1 數據庫中的集合名稱, 不存在會建立.

建立Model

db.model(「test1」, TestSchema );

1.構造函數, 參數1:集合名稱, 參數2:Schema實例

var Kitty = db.model('Kitty', mongooseSchema);

查詢

model.find({}, callback);
參數1忽略,或爲空對象則返回全部集合文檔

mongooseSchema.statics.findbytitle = function(title, callback) {
  return this.model('mongoose').find({title: title}, callback);
}
// executing a query explicitly
var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
query.exec(function (err, docs) {});
// using the promise returned from executing a query
var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
var promise = query.exec();
promise.addBack(function (err, docs) {});
var ff = mongooseModel.find({ username: 'model_demo_username'},function(e,r){
        console.log('ff',r)
      });
// named john and at least 18
MyModel.find({ name: 'john', age: { $gte: 18 }});

// executes immediately, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

model.find({},field,callback);

過濾查詢,參數2: {‘name’:1, ‘age’:0} 查詢文檔的返回結果包含name , 不包含age.(_id默認是1)

// name LIKE john and only selecting the "name" and "friends" fields, executing immediately
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })

model.find({},null,{limit:20});

過濾查詢,參數3: 遊標操做 limit限制返回結果數量爲20個,如不足20個則返回全部.

// passing options
MyModel.find({ name: /john/i }, null, { skip: 10 })

// passing options and executing immediately
MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});

model.findOne({}, callback);

查詢找到的第一個文檔

// find one iphone adventures - iphone adventures??
Adventure.findOne({ type: 'iphone' }, function (err, adventure) {});

// same as above
Adventure.findOne({ type: 'iphone' }).exec(function (err, adventure) {});

// select only the adventures name
Adventure.findOne({ type: 'iphone' }, 'name', function (err, adventure) {});

// same as above
Adventure.findOne({ type: 'iphone' }, 'name').exec(function (err, adventure) {});

// specify options, in this case lean
Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }, callback);

// same as above
Adventure.findOne({ type: 'iphone' }, 'name', { lean: true }).exec(callback);

// chaining findOne queries (same as above)
Adventure.findOne({ type: 'iphone' }).select('name').lean().exec(callback);

model.findById(‘obj._id’, callback);
查詢找到的第一個文檔,同上. 可是隻接受 __id 的值查詢

// find adventure by id and execute immediately
Adventure.findById(id, function (err, adventure) {});

// same as above
Adventure.findById(id).exec(callback);

// select only the adventures name and length
Adventure.findById(id, 'name length', function (err, adventure) {});

// same as above
Adventure.findById(id, 'name length').exec(callback);

// include all properties except for `length`
Adventure.findById(id, '-length').exec(function (err, adventure) {});

// passing options (in this case return the raw js objects, not mongoose documents by passing `lean`
Adventure.findById(id, 'name', { lean: true }, function (err, doc) {});

// same as above
Adventure.findById(id, 'name').lean().exec(function (err, doc) {});

建立

Model.create(文檔數據, callback))
在集合中建立一個文檔

// 增長記錄 基於model操做
var doc = {username : 'model_demo_username', title : 'model_demo_title', content : 'model_demo_content'};
mongooseModel.create(doc, function(error){
    if(error) {
        console.log(error);
    } else {
        console.log('save ok');
    }
    // 關閉數據庫連接
    db.close();
});

更新

Model.update(conditions, update, function(error)
參數1:查詢條件, 參數2:更新對象,可使用MondoDB的更新修改器

//修改記錄
//mongooseModel.update(conditions, update, options, callback);
var conditions = {username : 'model_demo_username'};
var update     = {$set : {age : 27, title : 'model_demo_title_update'}};
var options    = {upsert : true};
mongooseModel.update(conditions, update, options, function(error){
   if(error) {
       console.log(error);
   } else {
       console.log('update ok!');
   }
   //關閉數據庫連接
   db.close();
});

刪除

Model.remove(conditions,callback);
參數1:查詢條件

// 刪除記錄
var conditions = {username: 'emtity_demo_username'};
mongooseModel.remove(conditions, function(error){
    if(error) {
        console.log(error);
    } else {
        console.log('delete ok!');
    }

    //關閉數據庫連接
    db.close();
});

Model.where()

User.find({age: {$gte: 21, $lte: 65}}, callback);
等於:
User.where('age').gte(21).lte(65).exec(callback);

Entity

1.構造函數, 其實就是model的實例

new TestModel( { name:‘xueyou’, age:21 } );

2.建立, 在集合中建立一個文檔.

Entity.save(callback);

由Model建立的實體,使用save方法保存數據,Model和Entity都有能影響數據庫的操做,但Model比Entity更具操做性

var TestEntity = new TestModel({
       name : "Lenka",
       age  : 36,
       email: "lenka@qq.com"
});
console.log(TestEntity.name); // Lenka
console.log(TestEntity.age); // 36
var mongooseModel = db.model('mongoose', mongooseSchema);
var doc = {username : 'model_demo_username', title : 'emtity_demo_title', content : 'emtity_demo_content'};
var mongooseEntity = new mongooseModel(doc);
mongooseEntity.save(function(error) {
    if(error) {
        console.log(error);
    } else {
        console.log('saved OK!');
    })

修改器和更新器

更新修改器:

‘$inc’ 增減修改器,只對數字有效.下面的實例: 找到 age=22的文檔,修改文檔的age值自增1

Model.update({‘age’:22}, {’$inc’:{‘age’:1} }  );
執行後: age=23

‘$set’ 指定一個鍵的值,這個鍵不存在就建立它.能夠是任何MondoDB支持的類型.

Model.update({‘age’:22}, {’$set’:{‘age’:‘haha’} }  );
執行後: age=‘haha’

‘$unset’ 同上取反,刪除一個鍵

Model.update({‘age’:22}, {’$unset’:{‘age’:‘haha’} }  );
執行後: age鍵不存在

數組修改器:

‘$push’ 給一個鍵push一個數組成員,鍵不存在會建立

Model.update({‘age’:22}, {’$push’:{‘array’:10} }  );
執行後: 增長一個 array 鍵,類型爲數組, 有一個成員 10

‘$addToSet’ 向數組中添加一個元素,若是存在就不添加

Model.update({‘age’:22}, {’$addToSet’:{‘array’:10} }  );
執行後: array中有10因此不會添加

‘$each’ 遍歷數組, 和 $push 修改器配合能夠插入多個值

Model.update({‘age’:22}, {’$push’:{‘array’:{’$each’: [1,2,3,4,5]}} } );
執行後: array : [10,1,2,3,4,5]

‘$pop’ 向數組中尾部刪除一個元素

Model.update({‘age’:22}, {’$pop’:{‘array’:1} }  );
執行後: array : [10,1,2,3,4]  tips: 將1改爲-1能夠刪除數組首部元素

‘$pull’ 向數組中刪除指定元素

Model.update({‘age’:22}, {’$pull’:{‘array’:10} }  );
執行後: array : [1,2,3,4]  匹配到array中的10後將其刪除

條件查詢

「$lt」 小於
「$lte」 小於等於
「$gt」 大於
「$gte」 大於等於
「$ne」 不等於

Model.find({「age」:{ 「$get」:18 , 「$lte」:30 } } );
查詢 age 大於等於18並小於等於30的文檔

或查詢 OR

‘$in’ 一個鍵對應多個值
‘$nin’ 同上取反, 一個鍵不對應指定值
「$or」 多個條件匹配, 能夠嵌套 $in 使用
「$not」 同上取反, 查詢與特定模式不匹配的文檔
Model.find({「age」:{ 「$in」:[20,21,22.‘haha’]} } );
查詢 age等於20或21或21或’haha’的文檔

Model.find({"$or" :  [ {‘age’:18} , {‘name’:‘xueyou’} ] });
查詢 age等於18 或 name等於’xueyou’ 的文檔

類型查詢

null 能匹配自身和不存在的值, 想要匹配鍵的值 爲null, 就要經過 「$exists」 條件斷定鍵值已經存在
"$exists" (表示是否存在的意思)

Model.find(「age」 :  { 「$in」 : [null] , 「exists」 : true  } );
查詢 age值爲null的文檔

Model.find({name: {$exists: true}},function(error,docs){
  //查詢全部存在name屬性的文檔
});

Model.find({telephone: {$exists: false}},function(error,docs){
  //查詢全部不存在telephone屬性的文檔
});

正則表達式

MongoDb 使用 Prel兼容的正則表達式庫來匹配正則表達式

find( {「name」 : /joe/i } )    
查詢name爲 joe 的文檔, 並忽略大小寫

 find( {「name」 : /joe?/i } )
查詢匹配各類大小寫組合

查詢數組

Model.find({「array」:10} );
查詢 array(數組類型)鍵中有10的文檔, array : [1,2,3,4,5,10] 會匹配到

Model.find({「array[5]」:10} );
查詢 array(數組類型)鍵中下標5對應的值是10, array : [1,2,3,4,5,10] 會匹配到

‘$all’ 匹配數組中多個元素

Model.find({「array」:[5,10]} );
查詢 匹配array數組中 既有5又有10的文檔

‘$size’ 匹配數組長度

Model.find({「array」:{"$size" : 3} } );
查詢 匹配array數組長度爲3 的文檔

‘$slice’ 查詢子集合返回

Model.find({「array」:{"$skice" : 10} } );
查詢 匹配array數組的前10個元素
Model.find({「array」:{"$skice" : [5,10] } } );
查詢 匹配array數組的第5個到第10個元素

where

用它能夠執行任意javacript語句做爲查詢的一部分,若是回調函數返回 true 文檔就做爲結果的一部分返回

find( {"$where" : function(){
        for( var x in this ){
         //這個函數中的 this 就是文檔
        }
        
        if(this.x !== null && this.y !== null){
            return this.x + this.y === 10 ? true : false;
        }else{
            return true;
        }
        
        
}  }  )

簡化版本

find( {"$where" :  "this.x + this.y === 10" } )
    find( {"$where" : " function(){ return this.x + this.y ===10; } " } )

遊標

  1. limit(3) 限制返回結果的數量,
  2. skip(3) 跳過前3個文檔,返回其他的
  3. sort( {「username」:1 , 「age」:-1 } ) 排序 鍵對應文檔的鍵名, 值表明排序方向, 1 升序, -1降序
相關文章
相關標籤/搜索