mongdb练习---wuwangqiang----3--查询知识

三 查询知识
注:以下查询基于ecshop网站的商品表(ecs_goods)
在练习时可以只取部分列,方便查看.

1: 基础查询 where的练习:

查出满足以下条件的商品
1.1:主键为32的商品
db.goods.find({goods_id:32});

1.2:不属第3栏目的所有商品(KaTeX parse error: Expected '}', got 'EOF' at end of input: ….find({cat_id:{ne:3}},{goods_id:1,cat_id:1,goods_name:1});

1.3:本店价格高于3000元的商品{KaTeX parse error: Expected 'EOF', got '}' at position 3: gt}̲ db.goods.find…gt:3000}},{goods_name:1,shop_price:1});

1.4:本店价格低于或等于100元的商品(KaTeX parse error: Expected '}', got 'EOF' at end of input: …d({shop_price:{lte:100}},{goods_name:1,shop_price:1});

1.5:取出第4栏目或第11栏目的商品(KaTeX parse error: Expected '}', got 'EOF' at end of input: ….find({cat_id:{in:[4,11]}},{goods_name:1,shop_price:1});

1.6:取出100<=价格<=500的商品(KaTeX parse error: Expected '}', got 'EOF' at end of input: …db.goods.find({and:[{price:{KaTeX parse error: Expected 'EOF', got '}' at position 7: gt:100}̲,{price:{$lt:500}}}]);

1.7:取出不属于第3栏目且不属于第11栏目的商品($and n i n 和 nin和 ninnor分别实现)
db.goods.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: and:[{cat_id:{ne:3}},{cat_id:{KaTeX parse error: Expected 'EOF', got '}' at position 6: ne:11}̲}]},{goods_name…nin:[3,11]}},{goods_name:1,cat_id:1});
db.goods.find({$nor:[{cat_id:3},{cat_id:11}]},{goods_name:1,cat_id:1});

1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品()
db.goods.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: or:[{and:[{shop_price:{KaTeX parse error: Expected 'EOF', got '}' at position 7: gt:100}̲},{shop_price:{lt:300}}]},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …:[{shop_price:{gt:4000}},{shop_price:{$lt:5000}}]}]},{goods_name:1,shop_price:1});

1.9:取出goods_id%5 == 1, 即,1,6,11,…这样的商品
db.goods.find({goods_id:{$mod:[5,1]}});

1.10:取出有age属性的文档
db.stu.find({age:{$exists:1}});
含有age属性的文档将会被查出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值