三 查詢知識
注:以下查詢基於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 nor分別實現)
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屬性的文檔將會被查出