aldinokemal

MongoDB - Logical Operator

Mar 27th, 2022
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // select * from products where category in ('laptop', 'handphone') and price > 20000000
  2. db.products.find({
  3.     $and: [
  4.         {
  5.             category: {
  6.                 $in: ["laptop", "handphone"]
  7.             }
  8.         },
  9.         {
  10.             price: {
  11.                 $gt: 20000000
  12.             }
  13.         }
  14.     ]
  15. });
  16.  
  17. // select * from products where category not in ('laptop', 'handphone')
  18. db.products.find({
  19.     category: {
  20.         $not: {
  21.             $in: ["laptop", "handphone"]
  22.         }
  23.     }
  24. });
  25.  
  26. // select * from products where price between 10000000 and 20000000 and category != 'food'
  27. db.products.find({
  28.     $and: [
  29.         {
  30.             price: {
  31.                 $gte: 10000000,
  32.                 $lte: 20000000
  33.             }
  34.         },
  35.         {
  36.             category: {
  37.                 $ne: 'food'
  38.             }
  39.         }
  40.     ]
  41. });
Advertisement
Add Comment
Please, Sign In to add comment