aldinokemal

MongoDB - Comparison Operator

Mar 27th, 2022 (edited)
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // select * from customers where _id = 'ak'
  2. db.customers.find({
  3.     _id: {
  4.         $eq: "ak"
  5.     }
  6. });
  7.  
  8. // select * from products where price > 1000
  9. db.products.find({
  10.     price: {
  11.         $gt: 1000
  12.     }
  13. });
  14.  
  15. // insert product documents
  16. db.products.insertMany([
  17.     {
  18.         _id: 3,
  19.         name: "Pop Mie Rasa Bakso",
  20.         price: new NumberLong('2500'),
  21.         category: "food"
  22.     },
  23.     {
  24.         _id: 4,
  25.         name: "Samsung Galaxy S9+",
  26.         price: new NumberLong('10000000'),
  27.         category: "handphone"
  28.     },
  29.     {
  30.         _id: 5,
  31.         name: "Acer Precator XXI",
  32.         price: new NumberLong('25000000'),
  33.         category: "laptop"
  34.     }
  35. ]);
  36.  
  37. // select * from products where category in ('handphone', 'laptop') and price > 5000000
  38. db.products.find({
  39.     category: {
  40.         $in: ["handphone", "laptop"]
  41.     },
  42.     price: {
  43.         $gt: 5000000
  44.     }
  45. });
Add Comment
Please, Sign In to add comment