1、介绍
Mongoose 是一个对象文档模型库,官网 http://www.mongoosejs.net/
2、作用
方便使用代码操作 mongodb 数据库
3、使用流程
3.1、链接数据库
//1. 安装 mongoose---> npm install mongoose --save//2. 导入 mongoose
const mongoose = require("mongoose");
//3. 连接数据库 mongodb://127.0.0.1:27017+数据库名字
mongoose.connect("mongodb://127.0.0.1:27017/col");
//4. 设置连接回调
//连接成功
mongoose.connection.on("open", () => {console.log("连接成功");
});//连接出错
mongoose.connection.on("error", () => {console.log("连接出错~~");
});//连接关闭
mongoose.connection.on("close", () => {console.log("连接关闭");
});
3.2、在数据表里面添加数据
字段类型:
//2. 导入 mongoose
const mongoose = require("mongoose");
//3. 连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/col");//4. 设置连接回调
//连接成功
mongoose.connection.on("open", () => {console.log("连接成功");// 声明数据量文档的字段类型。类似ts的定义const bookSchema = new mongoose.Schema({id: Number,name: String,num: Number,});// 声明往那个集合(表)添加数据,没有该集合会自动创建,这里集合名字books,并定义集合的文档类型const bookModel = mongoose.model("book", bookSchema);// 往该集合添加数据bookModel.insertMany([{id: 6,name: "多多",num: 2,},{id: 7,name: "公告",num: 5,},{id: 8,name: "更换",num: 4,},{id: 9,name: "观后感",num: 8,},]).then((data) => {console.log("插入文本success", data);// mongoose.connection.close();}).catch((err) => {if (err) throw err;});
});//连接出错
mongoose.connection.on("error", () => {console.log("连接出错~~");
});//连接关闭
mongoose.connection.on("close", () => {console.log("连接关闭");
});
// setTimeout(() => {
// // 断开与 MongoDB 的连接并关闭数据库
// mongoose.connection.close();
// mongoose.disconnect();
// }, 2000);
3.3、删除数据
//2. 导入 mongoose
const mongoose = require("mongoose");
//3. 连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/col");
//4. 设置连接回调
//连接成功
mongoose.connection.on("open", () => {console.log("连接成功");const bookSchema = new mongoose.Schema({id: Number,name: String,num: Number,});const bookModel = mongoose.model("book", bookSchema);// 删除books集合中,文档id为3的一条数据bookModel.deleteOne({ id: 3 }).then((data) => {console.log("删除成功", data);}).catch((err) => {console.log("删除失败");});// 删除books集合中,满足条件num为55的所有文档// bookModel// .deleteMany({ num: 55 })// .then((data) => {// console.log("删除成功", data);// })// .catch((err) => {// console.log("删除失败");// });
});//连接出错
mongoose.connection.on("error", () => {console.log("连接出错~~");
});//连接关闭
mongoose.connection.on("close", () => {console.log("连接关闭");
});
3.4、修改数据
//2. 导入 mongoose
const mongoose = require("mongoose");
//3. 连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/col");
//4. 设置连接回调
//连接成功
mongoose.connection.on("open", () => {console.log("连接成功");const bookSchema = new mongoose.Schema({id: Number,name: String,num: Number,});const bookModel = mongoose.model("book", bookSchema);// 更新一条数据bookModel.updateOne({ id: 2 }, { name: "22" }).then((data) => {console.log("更新成功", data);mongoose.connection.close();}).catch((err) => {console.log("更新失败");mongoose.connection.close();});// 更新多条数据,符合条件的数据全部更新// bookModel// .updateMany({ id: 2 }, { name: "22" })// .then((data) => {// console.log("更新成功", data);// mongoose.connection.close();// })// .catch((err) => {// console.log("更新失败");// mongoose.connection.close();// });
});//连接出错
mongoose.connection.on("error", () => {console.log("连接出错~~");
});//连接关闭
mongoose.connection.on("close", () => {console.log("连接关闭");
});
3.5、查找数据
//2. 导入 mongoose
const mongoose = require("mongoose");
//3. 连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/col");
//4. 设置连接回调
//连接成功
mongoose.connection.on("open", () => {console.log("连接成功");const bookSchema = new mongoose.Schema({id: Number,name: String,num: Number,});const bookModel = mongoose.model("book", bookSchema);// 读取单条bookModel.findOne({ id: 2 }).then((data) => {console.log("读取成功", data);}).catch((err) => {console.log("读取失败");});//根据 ID 获取文档// bookModel// .findById("63f34af50cf203761ede1896")// .then((data) => {// console.log("读取成功", data);// })// .catch((err) => {// console.log("读取失败");// });//读取所有// bookModel// .find()// .then((data) => {// console.log("读取成功", data);// })// .catch((err) => {// console.log("读取失败");// });
});//连接出错
mongoose.connection.on("error", () => {console.log("连接出错~~");
});//连接关闭
mongoose.connection.on("close", () => {console.log("连接关闭");
});
3.6、 条件查询
//价格小于 20 的图书BookModel.find({ price: { $lt: 20 } }).then((data) => {console.log("查询成功", data);mongoose.connection.close();}).catch((err) => {console.log("查询失败");mongoose.connection.close();});//曹雪芹 或者 余华的书// BookModel.find({ $or: [{ author: "曹雪芹" }, { author: "余华" }] })// .then((data) => {// console.log("查询成功", data);// mongoose.connection.close();// })// .catch((err) => {// console.log("查询失败");// mongoose.connection.close();// });//价格大于 30 且小于 70// BookModel.find({ $and: [{ price: { $gt: 30 } }, { price: { $lt: 70 } }] });// .then((data) => {// console.log("查询成功", data);// mongoose.connection.close();// })// .catch((err) => {// console.log("查询失败");// mongoose.connection.close();// });//正则表达式, 搜索书籍名称中带有 `三` 的图书// BookModel.find({ name: /三/ });// .then((data) => {// console.log("查询成功", data);// mongoose.connection.close();// })// .catch((err) => {// console.log("查询失败");// mongoose.connection.close();// });
3.7、个性化读取
(1)字段筛选
//0:不要的字段
//1:要的字段
SongModel.find().select({_id:0,title:1}).exec(function(err,data){
if(err) throw err;
console.log(data);
mongoose.connection.close();
});
(2)数据排序
//sort 排序
//1:升序
//-1:倒序
SongModel.find().sort({hot:1}).exec(function(err,data){
if(err) throw err;
console.log(data);
mongoose.connection.close();
});
(3)数据截取
//skip 跳过 limit 限定
SongModel.find().skip(10).limit(10).exec(function(err,data){
if(err) throw err;
console.log(data);
mongoose.connection.close();
});