MongoDB中的增删改查 初学

MongoDB增删改查

**//查询用户中的所有文档**
User.find().then(result=>{
console.log(result)
})
**//查询名字为李四的文档**
User.find({name:'李四'}).then(result=>{
console.log(result)
})
**//查询爱好中有足球的文档**
User.find({hobbies:{$in:['足球']}}).then(result=>{
	console.log(result)
})
**//查询年龄中大于20且小于50的文档**
User.find({age:{$gt:20,$lt:50}}).then(result=>{
console.log(result)
})
//查询要查询的字段  不是把所有字段都显示出来的就用-号表示   id字段会默认显示出来
User.find().select('name email -id').then(result=>{
	console.log(result)
})
//根据age字段进行升序排序
User.find().sort('age').then(result=>{
	console.log(result)
})
//根据age字段进行升序排序
User.find().sort('-age').then(result=>{
	console.log(result)
})

User.find().skip(2).limit(3).then(result=>{
	console.log(result)
})




//删除数据
User.findOneAndDelete({name:'张三'}).then(result=>{
	console.log(result)
});
//删除多条数据
User.DeleteMany().then(result=>{
	console.log(result)
});



//更新数据
User.updateOne({ name: '李四' }, { name: '李狗蛋' }).then(result => {
    console.log(result);
});
//更新集合中的文档(更新多个)
User.updateMany({}, { age: 56 }).then(result => {
    console.log(result);
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值