数据存放路径:D:\data\db
安装目录:D:\MongoDB\mongodb-windows-x86_64-5.0.19\mongodb-win32-x86_64-windows-5.0.19\bin 配置环境变量
# 开启服务端,指定数据存放路径
mongod --dbpath D:\data\db
# 开启客户端
mongo
# 显示所有数据库
show dbs
# 切换数据库,没有则自动创建
use testDatabase
# 当前所在数据库
db
# 删除数据库(切换到要删除的数据库)
use testDatabase
db.dropDatabase()
# 创建集合
db.createCollection('users')
# 显示所有集合
show collections
# 删除集合
db.集合名.drop()
# 集合重命名
db.集合名.renameCollection('新集合名')
# 插入文档
db.集合名.insert(文档对象)
# 查询文档
# _id是mongodb自动生成的唯一编号,用来唯一标识文档
db.集合名.find(查询条件)
db.集合名.find() # 显示所有文档
# 更新文档
db.集合名.update(查询条件,新文档对象)
db.集合名.update({name:"ccc"},{$set:{age:999}})
# 删除文档
db.集合名.remove(查询条件)
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/testDatabase");
mongoose.connection.once("open", () => {
console.log("连接成功");
});
mongoose.connection.on("error", () => {
console.log("连接失败");
});
mongoose.connection.on("close", () => {
console.log("连接关闭");
});
// 关闭连接
mongoose.disconnect();
// 方式1 只有字段类型
// 创建文档的结构schema
const bookSchema1 = new mongoose.Schema({
name: String,
author: String,
price: Number,
tags: Arrary
});
// 方式2 添加字段校验
const bookSchema1 = new mongoose.Schema({
name: {
type: String,
required: true // 必填
},
author: {
type: String,
default: '匿名' // 默认值
},
price: {
type: Number,
unique: true // 唯一值
},
tags: {
type: Arrary,
enum: ['a','b'] // 枚举值
},
});
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/testDatabase");
mongoose.connection.once("open", () => {
console.log("连接成功");
// 1.创建文档的结构schema
const bookSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
author: {
type: String,
default: "匿名作者",
},
price: Number,
});
// 2.创建文档的模型对象,用于操作文档
const bookModel = mongoose.model("booksCollection", bookSchema);
// 3.使用文档的模型对象操作文档(增删改查)
// 插入
bookModel
.create({
name: "bookA",
price: 88888888,
})
.then((res) => {
console.log("单条插入", res);
})
.catch((err) => {
console.log(err);
});
bookModel
.insertMany([
{
name: "西游记",
author: "a",
price: 1,
},
{
name: "红楼梦",
author: "b",
price: "2",
},
{
name: "三国演义",
author: "c",
price: 3,
},
{
name: "水浒传",
author: "d",
price: 4,
},
])
.then((res) => {
console.log("批量插入", res);
})
.catch((err) => {
console.log(err);
});
// 删除
bookModel.deleteOne({ name: "bookA" }).then((res) => {
console.log("删除", res);
});
// 更新
bookModel.updateOne({ name: "西游记" }, { price: 123456 }).then((res) => {
console.log("更新", res);
});
// 查找
bookModel.find().then((res) => {
console.log("查找", res);
});
});
mongoose.connection.on("error", () => {
console.log("连接失败");
});
mongoose.connection.on("close", () => {
console.log("连接关闭");
});
// 关闭连接
// mongoose.disconnect();
// select 1需要的字段 0不要的字段
// sort 1升序 -1降序
// skip 跳过1条对象
// limit 前n条对象
bookModel
.find()
.select({ _id: 0, name: 1, price: 1 })
.sort({ price: -1 })
.skip(1)
.limit(3)
.then((res) => {
console.log("查找", res);
});
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/testDatabase");
mongoose.connection.once("open", () => {
console.log("连接成功");
// 1.创建文档的结构schema
const bookSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
author: {
type: String,
default: "匿名作者",
},
price: Number,
});
// 2.创建文档的模型对象,用于操作文档
const bookModel = mongoose.model("booksCollection", bookSchema);
// 3.使用文档的模型对象操作文档
// 查找
bookModel
.find()
.select({ _id: 0, name: 1, price: 1 })
.sort({ price: -1 })
.skip(1)
.limit(3)
.then((res) => {
console.log("查找", res);
});
});
mongoose.connection.on("error", () => {
console.log("连接失败");
});
mongoose.connection.on("close", () => {
console.log("连接关闭");
});