MongoDB 的安装
windows环境下安装
MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mongodb.com/download-center/community
MongoDB 图形化工具安装
MongoDB Compass 是一个图形界面管理工具,我们可以在后面自己到官网下载安装,下载地址:https://www.mongodb.com/download-center/compass。
Linux环境安装
下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) 。
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz # 下载
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz # 解压
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb # 将解压包拷贝到指定目录
MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:
export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> 为你 MongoDB 的安装路径。如本文的 /usr/local/mongodb 。
以上部分可以自己查阅资料安装配置,本文章仅介绍Mongoose最常用的操作方法。
Mongoose手册:https://mongoosejs.com/docs/guides.html
node.js操作数据库的npm 包安装
npm install mongoose -g
使用:
const mongoose = require('mongoose);
mongoose.connect('mongodb://localhost/videos',{useNewUrlParser: true,useUnifiedTopology: true})
.then(()=>console.log('数据库连接成功'))
.catch(err=>console.log(err,'数据库连接失败'));
const courseSchema = new mongoose.Schema({
name:String,
author:String,
isPublished:Boolean
});
const Course = mongoose.model('Course',courseSchema);
const course = new Course({
name:'前端全栈',
author:'jack',
isPublished:true
});
course.save();
创建文档操作
//方法1
Course.create({
name:'js课程',
author:'jack',
isPublished:true
},function(err,result){
console.log(err);
console.log(result);
})
//方法2
Course.create({
name:'js课程',
author:'jack',
isPublished:true
})
.then(result=>{
console.log(result);
})
.catch(err=>console.log(err))
字段验证
name:{
type:String,
required:[true,"请输入文章标题'],
minlength:[2,'文章长度不能小于2'],
maxlength:[5,'文章长度最大不能超过5'],
trim:true,
validate:;
default:默认值
enum:['a','b']
}
types:
required:true 不能为空 (必填项)
minlenght: 2 字符串最小长度
maxlenght:20 字符串最大长度
min:2 数值最小值
max:100 数值最大值
enum:['a','b'] 枚举数组中的其中一个
validate: 自定义验证器
default: 默认值
导入数据
mongoimport -d 数据库名称 -c 集合名称 -file 要导入的数据文件
查询文档
User.find({age:{$gt:20,$lt:50}}).then(result=>console.log(result))
//匹配包含
User.find({hobbies:{$in:['篮球']}}).then(result=>{})
//选择字段查询
User.find().select('name email').then(result=>{})
//将数据按照年龄进行排序
User.find().sort('age').then(result=>{})
User.find().sort('-age').then(result=>{})
//分页,limit
User.find().skip(2).limit(2).then(result => {})
//获取一条数据
User.findOne( ).then(result=>console.log(result))
删除文档
Course.findOneAndDelete({}).then(result=>{})
Course.findOneAndDelete({_id:'adsfad'}).then(result=>{})
Course.deleteMany({}).then(result=>{})
更新update
User.updateOne({条件},{要修改的值}).then(result=>{})
User.updateMany({条件},{要修改的值}).then(result=>{})
User.updateOne({username:'jack',{age:22,hobbies:'篮球'}}).then(result=>console(result))
User.updateMany({},{age:56}).then(result=>console.log(result))
集合关联实现
//用户模型
const User = mongoose.model('User',new mongoose.Schema({
name:{
type:String
}
}));
//文章模型
const Article = mongoose.model('Article',new mongoose.Schema({
title:{type:String},
author:{
type:mongoose.Schema.Types.ObjectId,
ref:'User' //关联User模型
}
}));
Article.find().populate('author').then(err,result=>console.log(result));