MongoDB是一个开源的、文档型的NoSQL数据库程序。MongoDB将数据存储在类似JSON的文档中,操作起来更灵活方便。NoSQL数据库中的文档(documents)对应于SQL数据库中的一行。将一组文档组合在一起称为集合(collections),它大致相当于关系数据库中的表。
除了作为一个NoSQL数据库,MongoDB还有一些自己的特性:
在Node.js中,通常使用Mongoose库对MongoDB进行操作。Mongoose是一个MongoDB对象建模工具,设计用于在异步环境中工作。
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground')
.then(() => console.log('Connected to MongoDB...'))
.catch( err => console.error('Could not connect to MongoDB... ', err));
Mongoose中的一切都始于一个模式。每个模式都映射到一个MongoDB集合,并定义该集合中文档的形状。
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: {type: Date, default: Date.now},
isPublished: Boolean
});
模型是根据模式定义编译的构造函数,模型的实例称为文档,模型负责从底层MongoDB数据库创建和读取文档。
const Course = mongoose.model('Course', courseSchema);
const course = new Course({
name: 'Nodejs Course',
author: 'Hiram',
tags: ['node', 'backend'],
isPublished: true
});
async function createCourse(){
const course = new Course({
name: 'Nodejs Course',
author: 'Hiram',
tags: ['node', 'backend'],
isPublished: true
});
const result = await course.save();
console.log(result);
}
createCourse();
async function getCourses(){
const courses = await Course
.find({author: 'Hiram', isPublished: true})
.limit(10)
.sort({name: 1})
.select({name: 1, tags:1});
console.log(courses);
}
getCourses();
async function getCourses(){
const courses = await Course
// .find({author: 'Hiram', isPublished: true})
// .find({ price: {$gt: 10, $lte: 20} })
.find({price: {$in: [10, 15, 20]} })
.limit(10)
.sort({name: 1})
.select({name: 1, tags:1});
console.log(courses);
}
getCourses();
async function getCourses(){
const courses = await Course
// .find({author: 'Hiram', isPublished: true})
.find()
// .or([{author: 'Hiram'}, {isPublished: true}])
.and([{author: 'Hiram', isPublished: true}])
.limit(10)
.sort({name: 1})
.select({name: 1, tags:1});
console.log(courses);
}
getCourses();
async function getCourses(){
const courses = await Course
// .find({author: 'Hiram', isPublished: true})
//author字段以“Hiram”开头
// .find({author: /^Hiram/})
//author字段以“Pierce”结尾
// .find({author: /Pierce$/i })
//author字段包含“Hiram”
.find({author: /.*Hiram.*/i })
.limit(10)
.sort({name: 1})
.select({name: 1, tags:1});
console.log(courses);
}
getCourses();
async function getCourses(){
const courses = await Course
.find({author: 'Hiram', isPublished: true})
.count();
console.log(courses);
}
getCourses();
通过结合使用 skip() 及 limit() 可以做到分页查询的效果
async function getCourses(){
const pageNumber = 2;
const pageSize = 10;
const courses = await Course
.find({author: 'Hiram', isPublished: true})
.skip((pageNumber - 1) * pageSize)
.limit(pageSize)
.sort({name: 1})
.select({name: 1, tags: 1});
console.log(courses);
}
getCourses();
async function updateCourse(id){
const course = await Course.findById(id);
if(!course) return;
course.isPublished = true;
course.author = 'Another Author';
const result = await course.save();
console.log(result);
}
async function updateCourse(id){
const course = await Course.findByIdAndUpdate(id, {
$set: {
author: 'Jack',
isPublished: false
}
}, {new: true}); //true返回修改后的文档,false返回修改前的文档
console.log(course);
}
MongoDB更新操作符,请参考:https://docs.mongodb.com/manual/reference/operator/update/
async function removeCourse(id){
// const result = await Course.deleteMany({ _id: id});
const course = await Course.findByIdAndRemove(id);
console.log(course)
}
原文:https://www.cnblogs.com/hiramP/p/10724945.html