import mongoose from 'mongoose'
// 1. 连接数据库
// 指定连接的数据库可以不存在,当你插入第一条数据之后就会自动被创建出来
mongoose.connect('mongodb://localhost/test')
// 2. 设计文档结构(表结构)
// 字段名称就是表结构中的属性名称
// 约束的目的是为了保证数据的完整性,不要有脏数据
const { Schema } = mongoose
const userSchema = new Schema({
username: {
type: String,
unique: true, //唯一值
required: true, // 必须有
index: true
},
password: {
type: String,
required: true
},
email: { type: String }
})
const blogSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
})
// 3. 将文档结构发布为模型
// mongoose.model 方法就是用来将一个架构发布为 model
// 第一个参数:传入一个大写名词单数字符串用来表示你的集合名称
// mongoose 会自动将大写名词的字符串生成 小写复数 的集合名称
// 例如这里的 User 最终会变为 users 集合名称
// 第二个参数:架构 Schema
// 返回值:模型构造函数
const User = mongoose.model('User', userSchema) //关联 users 的集合
//创建集合Cat,会自动创建集合cats
const Cat = mongoose.model('Cat', { name: String })
// 4. 当我们有了模型构造函数之后,就可以使用这个构造函数对 users 集合中的数据增删改查
//新增数据方法 create new
let res = await User.create({
username: 'jiang',
password: '55555',
email: '[email protected]'
})
console.log(566, res)
const admin = new User({
username: 'jiang',
password: '55555',
email: '[email protected]'
})
admin.save() //await admin.save()
.then((res) => console.log('新增成功', res))
.catch(error => console.log(44, error))
//查找数据 find findOne
User.find() //查找所有数据
.then((res) => console.log('查找成功', res)) //if(res.length == 0) 则为空
.catch(error => console.log(44, error))
//它总是返回一个数组[], 如果没有则会为空数组
//增加查找条件
or() :添加限制条件
limit(1) :查看几个
sort() :排序
skip():跳过几个
User.find()//查询全部
.or([{gender:2}])//查询gender为2的数据
.sort({_id:'desc'})//倒序排列数据, 或sort({_id: -1}),asc正序
.limit(2)//展示两条
.skip(2)//跳过前两个
User.find({
username: 'jiang' //查询条件
})
.then((res) => console.log('查找成功', res))
.catch(error => console.log(44, error))
User.findOne({
username: 'jiang'
})
.then((res) => console.log('查找成功', res)) // if(res == null) 则为空
.catch(error => console.log(44, error))
//成功则会返回一个对象,如果没有则为 null
//删除数据 deleteOne deleteMany
User.deleteOne({
username: 'jiang'
})
.then((res) => console.log('删除成功', res))
.catch(error => console.log(44, error))
// { acknowledged: true, deletedCount: 1 } 删除成功
// { acknowledged: true, deletedCount: 0 } 删除失败
//更新数据 save findOneAndUpdate
let res = await User.findOne({username:'jiang'})
res.username = 'li'
// res.set({username:'li'})
await res.save()
//findOneAndUpdate
[文档](https://blog.csdn.net/l_ppp/article/details/106092604)
Model.findOneAndUpdate(query, { $set: { name: 'jason bourne' }}, options)
await Balance.findOneAndUpdate({user_id: String(user._id)}, { $set: { balance: 5.33}})
***********
db.people.updateMany(
{ status: "C" },
{ $set: { age: 20 } }
)
User.findByIdAndUpdate('5a001b23d219eb00c8581184', {
username:'li'
}, function (err, ret) {
if (err) {
console.log('更新失败')
} else {
console.log('更新成功')
}
})
//删除集合 尽量少用
const res = await User.db.dropCollection('users') //删除User集合
console.log(544, res) //true
// let res = await Ipaddr.db.dropCollection('users') //在集合Ipaddr时删除User集合,这也是可以的
// let res = await Ipaddr.db.dropCollection('ipaddrs') //删除Ipaddr集合
Sort: Trending