Mongodb


mysqlmongodb edis

1 数据库

    持久化数据

Mongodb:json形式存储

安装必要的软件:mongodb + compass

    database    mongodb软件可以创建多个数据库

    collection  集合    一组数据

    document    文档    一条数据    json

    field   字段    文档属性   

数据导入数据库

    mongoimport -d 数据库 -c 集合名 --file 导入的数据文件

基础应用

let mongoose = require('mongoose');

// 建立数据库连接

mongoose.connect('mongodb://localhost/demo1', { useNewUrlParser: true, useUnifiedTopology: true })

    .then(() => {

console.log('数据库连接成功');

    }).catch((err) => {

console.log(err);

    });

//创建集合的规则

const teacherSchema = mongoose.Schema({

name: String,

tel: String,

age: Number

});

//创建符合规则的集合实例

const Teacher = mongoose.model('Teacher', teacherSchema);   //数据库默认将数据库名后+s

//创建文档

Teacher.create({

name: 'macro',

tel: '123456',

age: 25

}, (err, doc) => {

console.log(err);

console.log(doc);

});

mongoDB数据库查询

// 全表扫描

Teacher.find().then(res => {

console.log(res);

});

//通过id扫描

Teacher.find({ _id: '5eb74d758a8f28c0780f5fa0' }).then(res => {

console.log(res);

});

//通过姓名查找

Teacher.find({ name: '李鹤' }).then(res => {

console.log(res);

});

//查询第一个:默认返回集合中满足条件的第一条文档

Teacher.findOne({ name: '李鹤' }).then(res => {

console.log(res);

});

//  注意json格式书写

Teacher.find({ age: { $gte: 18, $lte: 30 } }).then(res => {

console.log(res);

});

//满足约束条件范围的结果集

//$in : 指代只要有一个就可以

Teacher.find({ hobbies: { $in: ['篮球', '乒乓球'] } }).then(res => {

console.log(res);

});

//查找所有学生的年龄和姓名

//-_id : 去掉id字段

Teacher.find().select('name age -_id').then(res => {

console.log(res);

});

//排序:默认升序

Teacher.find().sort('age').then(res => {

console.log(res);

});

//- 降序

Teacher.find().sort('-age').then(res => {

console.log(res);

});

//分页:skip(2):跳过前2条

// limit(2):最后两条

Teacher.find().skip(1).then(res => {

console.log(res);

});

//删除

Teacher.findOneAndDelete({ name: 'macro' });

//返回:找到满足条件的数据并且删除

Teacher.findOneAndDelete({ _id: '5eb74d758a8f28c0780f5fa0' }).then(res => {

console.log(res);

}).catch(err => {

console.log(err);

});

//删除多条数据

Teacher.deleteMany({ name: 'macro' }).then(res => {

console.log(res);

}).catch(err => {

console.log(err);

});


//修改

Teacher.updateOne({ name: 'macro' }, { age: 19, tel: 98989 }).then(res => {

console.log(res);

});

//修改多条/

//{限制条件}

Teacher.updateMany({}, { age: 19, tel: 98989 }).then(res => {

console.log(res);

});

//数据校验

//创建集合的规则

const teacherSchema = mongoose.Schema({

name: {

type: String,

required: [true, '请传入姓名'], //必传

minlength: [2, '姓名长度不能小于2'],

maxlength: [9, '姓名长度不能大于9'],

trim: true //去除字符串首尾空格

    },

tel: String,

age: {

type: Number,

min: 1,

max: 126

    },

hobbies: [String],

birthday: {

type: Date,

default: Date.now

    },

email: {

type: String,

enum: {

values: ['user@qq.com', 'macro@163.com'],

message: '邮箱地址必须从指定的选择'

        }

    },

qq: {

type: String,

validate: {

validator: v => {

return v.charAt(0) === '1' && v.length === 6;

            },

message: 'qq号必须以1开头且长度是6'

        }

    }

原文地址:https://www.cnblogs.com/macro-renzhansheng/p/13042754.html