下载地址:https://www.mongodb.com/try/download/community-kubernetes-operator
配置步骤:
show dbs
use 数据库名
db
use 数据库名
db.dropDatabase()
db.createCollection('world')
show collections
db.集合名.drop()
db.集合名.renameCollection('新集合名')
插入文档
``db.集合名.insert(文档对象)··
更新文档
db.集合名.update(查询条件,新的文档)
会用新文档直接替代全部旧文档的内容
db.集合名.update({name:'张三'},{$set:{age:33})
只更新特定属性的值
删除文档
db.集合名.remove(查询条件)
mongoose版本 ^7.6.3
// mongoose版本 ^7.6.3 ,其他版本可参考mongoose文档 https://mongoose.nodejs.cn/docs/index.html
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/test') //连接test这个集合,没有test集合则创建test集合
//mongoose.connection.once once 只连接一次
mongoose.connection.on('open',()=>{
console.log('连接成功');
})
mongoose.connection.on('error',()=>{
console.log('连接失败');
})
mongoose.connection.on('close',()=>{
console.log('连接关闭');
})
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/test')//连接test这个集合,没有test集合则创建test集合
//mongoose.connection.once once 只连接一次
mongoose.connection.on('open',()=>{
console.log('连接成功');
// 创建文档的结构对象
let BookSchema = new mongoose.Schema({
name:String,
age:Number,
sex:String
})
// 创建模型对象,对文档操作的封装对象
let BookSModel = mongoose.model('books',BookSchema)
// 新增
BookSModel.create({
name:'xiaoming',
age:23,
sex:'男'
}).then(res=>{
console.log(res);
}).catch(err=>{
console.log(err);
})
})
mongoose.connection.on('error',()=>{
console.log('连接失败');
})
mongoose.connection.on('close',()=>{
console.log('连接关闭');
})
类型 | 描述 |
---|---|
String | 字符串 |
Number | 数字 |
Boolean | 布尔值 |
Array | 数组,也可以使用[] 来表示 |
Date | 日期 |
Buffer | Buffer对象 |
Mixed | 任意类型,需要使用mongoose.Schema.Types.Mixed 指定 |
ObjectId | 对象ID,需要使用mongoose.Schema.Types.ObjectId 指定 |
Decimal128 | 高精度数字,需要使用mongoose.Schema.Types.Decimal128 指定 |
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/test')
//mongoose.connection.once once 只连接一次
mongoose.connection.on('open', () => {
console.log('连接成功');
// 创建文档的结构对象
let BookSchema = new mongoose.Schema({
name: String,
age: Number,
sex: String,
is_hot: Boolean,
arr: Array
})
// 创建模型对象,对文档操作的封装对象
let BookSModel = mongoose.model('noves', BookSchema)
// 新增
let data = [
{
name: 'xiaoming',
age: 23,
sex: '男',
is_hot: true,
arr: [
1, 2, 3
]
}, {
name: 'xiaobai',
age: 26,
sex: '女',
is_hot: true,
arr: [
4, 5
]
}, {
name: 'xiao1',
age: 26,
sex: '女',
is_hot: false,
arr: [
4, 5
]
},
{
name: 'xiao2',
age: 26,
sex: '女',
is_hot: false,
arr: [
4, 5
]
}
]
BookSModel.insertMany(data).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})
// 删除单条
// BookSModel.deleteOne({ _id: '657ffc52c123a96ba890ea75'}).then(res=>{
// console.log('res--->',res);
// })
// 批量删除
// BookSModel.deleteMany({ age: 23}).then(res=>{
// console.log('res--->',res);
// })
// 更新文档,更新一条
// BookSModel.updateOne({name:'xiaobai'},{arr: ['hello']}).then(res=>{
// console.log('res--->',res);
// })
// 批量更新
// BookSModel.updateMany({ name:'xiaobai'},{age:18}).then(res=>{
// console.log('res--->',res);
// })
// 读取文档
// BookSModel.findOne({ name:'xiao1'}).then(res=>{
// console.log('res--->',res);
// })
// BookSModel.findById('658001b0d56b0d19db15c768').then(res=>{
// console.log('res--->',res);
// })
BookSModel.find({arr:['hello']}).then(res=>{
console.log('res--->',res);
})
// BookSModel.find().then(res=>{
// console.log('res--->',res);
// })
})
在mongodb不能适应< > = 等运算符,需要使用替代符合
>
使用$gt
<
使用$lt
>=
使用$gte
<=
使用$lte
!==
使用$ne
示例:
BookSModel.find({age:{$lt:24}}).then(res=>{
console.log('res--->',res);
})
$or
逻辑或$and
逻辑与示例:
BookSModel.find({$or:[{name:'xiaoming'},{age:26}]}).then(res=>{
console.log('res--->',res);
})
可以使用js的正则语法,通过正则可进行模糊查询
示例:
BookSModel.find({name:/1/}).then(res=>{
console.log('res--->',res);
})
// 字段筛选 0 不要的字段,1 要的字段
BookSModel.find().select({name:1,age:1,sex:1}).then(res=>{
console.log('res--->',res);
})
// 数据排序 1 升序 , -1 降序
BookSModel.find().sort({age:-1}).then(res=>{
console.log('res--->',res);
})
// 数据截取 skip跳过,limit限定
BookSModel.find().select({name:1,age:1,sex:1}).skip(2).limit(3).then(res=>{
console.log('res--->',res);
})