Mongo DB :- Specify AND & OR Conditions

In this post I am going to explain how to Specify AND & OR Conditions from Mongo DB collection . for this as a sample data you have to upload data into mongo db database.

db.inventory.insertMany([
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

In the following example, the compound query document selects all documents in the collection where the status equals "A" and either qty is less than ($lt) 30 or item starts with the character p:

Then you can execute below query.
db.inventory.find( {status: "A“,$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]} )
Above selection command is identical to
SELECT * FROM inventory WHERE status = 'A' AND ( qty < 30 OR item LIKE 'p%');
 in the SQL


Thank You !!!

Mongo DB :- Specify AND & OR Conditions Mongo DB :- Specify AND & OR Conditions Reviewed by Pubudu Dewagama on 3:42:00 PM Rating: 5

3 comments:

  1. If you're looking to lose kilograms then you absolutely have to start following this totally brand new personalized keto plan.

    To create this keto diet, certified nutritionists, fitness trainers, and professional chefs have united to develop keto meal plans that are powerful, painless, money-efficient, and delicious.

    From their grand opening in January 2019, 100's of individuals have already remodeled their figure and health with the benefits a proper keto plan can give.

    Speaking of benefits: clicking this link, you'll discover 8 scientifically-tested ones provided by the keto plan.

    ReplyDelete