mongoodb(增删改查)crud

一.查找
1.查询用户集合中所有文档 [数组文档]
User.find().then(result=> console.log(result))
2.通过_id字段查找文档
user.find({_id:‘5f0accab42d90c49f86bdab9’}).then(result=>console.log(result))
3.findOne方法返回一条文档,默认返回集合第一条文档 {对象文档}
user.findOne({name:‘李四’}).then(result=>console.log(result))
4.查询用户集合中年龄字段大于X或者小于X的文档 匹配大于小于
user.find({age: {$gt:20, KaTeX parse error: Expected 'EOF', got '}' at position 6: lt:50}̲}).then(result=…in :[‘足球’]}})
6.旋转要查询的字段 选择要查询的字段
user.find().select(‘name eamil -_id’).then(result=>console.log(result))
7.将数据按照年龄大小进行升序排序
user.find(.sort(‘age’)).then(result=>console.log(result))
8.根据年龄字段进行降序排列
user.find(.sort(’-age’)).then(result=>console.log(result))
9.skip()跳过前俩条数据 limit限制查询2条数量
user.find().skip(2).limit(2).then(result=>console.log(result))
二. 2.0删除一个
user.findOneAndDelete({_id:‘5f0accab42d90c49f86bdab9’}).then(result=>console.log(result))
2.1删除多个
user.deleteMany({空对象会删除集合内所有的文档}).then(result=>console.log(result))
user.deleteMany({}).then(result=>console.log(result))
三. 更新文档 3.1更新单个
user.updateOne({查询条件},{要修改的值}).then(result=>console.log(result))
user.updateOne({name:‘张三’},{name:‘李四’}).then(result=>console.log(result))
3.2更新多个 查询条件为空所有的年龄都修改为56岁
user.updeteMany({},{age:56}).then(result=>console.log(result))
四.集合关联查找
post.find().populate(‘author’).then(result=>console.log(result))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值