Divide and Conquer
Estimated time to read: 1 minute
This isn't included in M001, however, the concept of Divide and Conquer is extremely useful in MongoDB.
Personally, I find that D&C is well suited for break down MQL queries and make them more versatile.
For example,
This is not a nice command to read or to write!
db.companies.countDocuments({ $or: [{ $and: [{ founded_year : 2004 },{ $or: [ { category_code : "social" }, { category_code : "web" } ] }]}, { $and: [{ founded_month : 10 },{ $or: [ { category_code : "social" }, { category_code : "web" } ] }]}] })
Using C&D, it can be broken down into variables and still return the same result.
year = { founded_year : 2004 };
month = { founded_month : 10 };
cat_social = { category_code : "social" };
cat_web = { category_code : "web" };
cat_search = { $or: [ cat_social, cat_web ] };
firstSub = { $and: [year,cat_search]};
secondSub = { $and: [month,cat_search]};
query = { $or: [firstSub, secondSub] };
db.companies.countDocuments(query)