//引入mongoose模块:
const mongoose = require('mongoose');
//连接数据库:
mongoose.connect('mongodb://127.0.0.1:27017/kinoko',(err)=>{
if(err){
console.log('连接失败')
}else{
console.log('连接成功')
}
//创建表以及字段类型:
//创建表user
const User = mongoose.model('user',{
//规定user表中的字段类型:
name:String,
age:Number
})
//增:
const user = new User({
name : '阿古',
age:19
})
const user1 = new User({
name : '美丽',
age:22
})
const user2 = new User({
name : '笑笑',
age:21
})
const user3 = new User({
name : '刘梅',
age:56
})
console.log(user.save()) //输出:Promise{<pending>}
user.save().then((result)=>{
console.log('成功的回调')
},()=>{
console.log('失败的回调')
})
user1.save().then((result)=>{
console.log('成功的回调')
},()=>{
console.log('失败的回调')
})
user2.save().then((result)=>{
console.log('成功的回调')
},()=>{
console.log('失败的回调')
})
user3.save().then((result)=>{
console.log('成功的回调')
},()=>{
console.log('失败的回调')
})
//删:
//删除指定对象:
User.remove({name:'美丽'}).then((result)=>{ //result:是一个对象 返回值是受影响条数
console.log(result)
})
//删除所有数据:
User.remove({}).then((result)=>{
console.log(result)
})
//删除指定ID
User.findByIdAndRemove("5c8263170998c51d58e14044").then((result)=>{ //5c8263170998c51d58e14044 ID值
console.log(result);
})
//改:
User.update({name:'阿古'},{$set:{name:"lily"}},{multi:true}).then((result)=>{
console.log(result); //multi:true 表示修改多条数据
})
User.findByIdAndUpdate("5c8263170998c51d58e14046",{$set:{name:'rose'}},{multi:true}).then((result)=>{
console.log(result);
})
//查找符合条件的所有数据:
User.find({name:'lily'}).then((result)=>{
console.log(result);
})
// //查询所有数据:
User.find().then((result)=>{
console.log(result);
})
//查询单条数据:
User.findOne({name:'lily'}).then((result)=>{
console.log(result);
})
//条件查询:$lt(小于) $lte(小于等于) $gt(大于) $gte(大于等于) $ne(不等于)
User.find({'age':{'$lt':20}}).then((result)=>{
console.log(result);
})
User.find({'age':{'$lte':20}}).then((result)=>{
console.log(result);
})
User.find({'age':{'$gt':20}}).then((result)=>{
console.log(result);
})
User.find({'age':{'$gte':20}}).then((result)=>{
console.log(result);
})
User.find({'age':{'$ne':20}}).then((result)=>{
console.log(result);
})
//$in(包含等于) $nin(不包含 不等于)
User.find({'age':{'$in':[18,19]}}).then((result)=>{
console.log(result)
})
User.find({'age':{'$nin':[18,19]}}).then((result)=>{
console.log(result)
})
$exists(判断当前关键字是否存在)
User.find({sex:{'$exists':true}}).then((result)=>{
console.log(result);
})
//查询指定列 如果不想要ID值 只需要设置ID = 0;
User.find({},{name:1,_id:0}).then((result)=>{
console.log(result);
})
//'$or'或
User.find({'$or':[{name:'lily'},{age:19}]}).then((result)=>{
console.log(result);
})
//$exists(判断当前关键字是否存在)
User.find({name:{'$exists':'true'}}).then((result)=>{
console.log(result)
})
//升序降序
User.find().sort({age:1}).then((result)=>{
console.log(result)
})
User.find().sort({age:-1}).then((result)=>{
console.log(result)
})
//模糊查询:
User.find({name:/l/}).then((result)=>{
console.log(result)
})
User.find({name:/^l/}).then((result)=>{ //以l开头
console.log(result)
})
User.find({name:/l$/}).then((result)=>{ //以l结尾
console.log(result)
})
//skip()查询n条以后的数据
User.find().skip(1).then((result)=>{
console.log(result)
})
//显示n到m之间的数据 skip跳过多少条,limit显示m-n条
User.find().skip(1).limit(3).then((result)=>{
console.log(result);
})
})
mongoose的操作及其常用命令
最新推荐文章于 2023-08-22 15:55:48 发布
本文档演示了使用mongoose库连接MongoDB数据库并执行增删改查操作的过程,包括创建用户表、保存数据、删除数据、更新数据以及各种查询条件的使用。涉及到的关键技术有mongoose模型、Promise、条件查询、排序、模糊匹配等。
292

被折叠的 条评论
为什么被折叠?



