查询表达式练习

  

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

  

1:基础查询的地方练习:

  

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

  

1.2:不属第3栏目的所有商品(ne)
db.goods.find ({cat_id: {$ ne: 3}}, {goods_id: 1、cat_id: 1、goods_name: 1});

  

1.3:本店价格高于3000元的商品{$ gt}
db.goods.find ({shop_price: {$ gt: 3000}}, {goods_name: 1、shop_price: 1});

  

1.4:本店价格低于或等于100元的商品($ lte)
db.goods.find ({shop_price: {$ lte: 100}}, {goods_name: 1、shop_price: 1});

  

1.5:取出第4栏目或第11栏目的商品(美元)
db.goods.find ({cat_id: {$: [4、11]}}, {goods_name: 1、shop_price: 1});

  

1.6:取出100 & lt;=价格& lt;=500的商品(和美元)
db.goods.find({$和:[{价格:{$ gt: 100},{美元价格:{$ lt: 500}}}]);

  

1.7:取出不属于第3栏目且不属于第11栏目的商品(和nin美元和美元也分别实现)
db.goods.find({$和:[{cat_id: {$ ne: 3}}, {cat_id: {$ ne: 11}}]}, {goods_name: 1、cat_id: 1})
db.goods.find ({cat_id:{$外祖母:[3,11]}},{goods_name: 1、cat_id: 1});
db.goods.find({$也:[{cat_id: 3}, {cat_id: 11}]}, {goods_name: 1、cat_id: 1});

  

1.8:取出价格大于100年且小于300年,或者大于4000年且小于5000年的商品()
db.goods.find({$或:[{$和:[{shop_price: {$ gt: 100}}, {shop_price: {$ lt: 300}}]},{$和:[{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:{$国防部:[5,1]}});

  

1.10:取出有时代属性的文档
db.stu.find({年龄:{$存在:1}});
含有年龄属性的文档将会被查出

  

1.11根据数据类型查询:
查询表达式练习

  

1.12:所有的美元用法:
查询表达式练习

查询表达式练习