node.js-mongoose

宰父衡
2023-12-01

mongoose

cnpm install -g mongoose

三方模块需要手动安装。

cnpm安装比较快,安装cnpm移步

source.js

const mongoose = require('mongoose');
const source_url = 'mongodb://localhost:27017/godme';
mongoose.connect(source_url);
mongoose.connection('connected', function(){
    console.log('connected success with source_url : ', source_url);
});
mongoose.connection('error', function(error){
    console.log('connect error : ', error);
});
mongoose.connection('disconnect', function(){
    console.log('disconnected !');
});
module.exports = mongoose;

mongoose:模块导入

source_url:连接地址

connect:发起连接

connection:监听事件

module.exports:模块导入

person.js

const mongoose = require('./source.js');
const Schema = mongoose.Schema;
var PeresonSchema = new Schema({
    name : {type : String},
    male : {type : String},
    age  : {type : Number}
});
module.exports = mongoose.model('Person', PersonSchema);

mongoose:同一个源

Schema:格式

PersonSchema:设置Person格式

model:生成模型

save.js

const Person = require('./person.js');
var person = new Person({
    name : 'godme',
    male : 'male',
    age  : 99
});
person.save(function(err, result){
    if(err){
        console.log('save error : ', err);
        return;
    }
    console.log('save success : ' , result);
})

Person:取出模型

person:通过模型直接new

save:对象直接保存,可设置回调方法

update.js

const Person = require('./person.js');
var where = {'name' : 'godme'}
var update = {'age' : '123'}
Person.update(where, update, function(error, result){
    if(error){
        console.log('update error : ', error);
        return;
    }
    console.log('update success : ', result);
});

Person:同源,静态调用即可

update:更新方法,先筛选,后更新,可回调

ID.js

const Person = require('./person.js');
var ID = '5bd95321242daf65e4347ea4';
var update = {'name' : 'judas'};
Person.findByIdAndUpdate(ID, update, function(error, result){
    if(error){
        console.log('update error : ', error);
        return;
    }
    console.log('update success : ', result);
});

findByIdAndUpdatewhere换成ID

delete.js

const Person = require('./person.js');
var where = {'age' : 123};
Person.remove(where, function(err, result){
    if(err){
        console.log('remove error : ', error);
        return;
    }
    console.log('remove result : ', result);
});

remove:即delete

where:筛查条件

search.js

const Person = require('./person.js');
var where = {'name' : 'godme'};
Person.find(where, function(err, result){
    if(err){
        console.log('find error : ', error);
        return;
    }
    console.log('find result : ', result);
});
  • options
const Person = require('./person.js');
var where = {'name' : 'godme'};
var options = {'age':0,'_id':0,'_v':0 };
Person.find(where, function(err, result){
    if(err){
        console.log('find error : ', error);
        return;
    }
    console.log('find result : ', result);
});

options中要么全1要么全0,交杂会报错。

  • 范围限定
{age: {$gte: 18, $lte: 30}}; // 18 <= age <= 30
  • id
var ID = '5bd95ae2272adb6bd01d91f9'
Person.findById(ID, function(err, result){
    if(err){
        console.log('find error : ', err);
        return;
    }
    console.log('find result : ', result);
});
  • count
Person.count({}, function(err, result){
    if(err){
        console.log('count error : ', err);
        return;
    }
    console.log('count : ', result);
})
  • 正则模糊
Person.find({'name' : {$regex : /^g/}}, function(err, result){
    if(err){
        console.log('find error : ', err);
        return;
    }
    console.log('find result : ', result);
})
  • 分页综合
const Person = require('./person.js');
var condition = {};
var show = {'_id':0,'__v':0,'male':0};
var pageSize = 5;
var order = {'age':1};
for(var currentPage = 0; currentPage < 5; currentPage++){
    var skipCount = currentPage * pageSize;
    Person.find(condition, show)
        .skip(skipCount)
        .limit(pageSize)
        .sort(order)
        .exec(function(err, resultList){
        if(err){
            console.log('page search Error : ', err);
            return;
        }
        console.log('page search size : ', resultList.length);
        resultList.forEach(function(result){
           console.log('- single : ', result); 
        });
    });
}

嗯,老操作,自我体会。

如果对mongo不太熟悉的,可以参阅一下这一篇,基本用法都有介绍。

index.js

const mongoose = require('./source.js');
const Schema = mongoose.Schema;
const PersonSchema = new Schema({
    name : {type : String, index : true},
    age  : {type : String},
    lastLogin : {type : Date , default : Date.now}
});
module.exports = mongoose.model('Person', PersonSchema);

index:指定为索引

default:设置默认值

 类似资料: