As an example, when we write db.students.find() and then do a forEach() loop over it, we are increasing the cost of the process as what we're doing is:
- Fetching all students from to server
- Running
forEachon our server to filter data we need
To avoid getting uncessary data and decrease the load on our server we can do requests specific to our needs, like these.
db.students.find({}, {name: 1, email: 0}) # values with 1 show up, 0 don't
db.students.find({}, {_id: 1, email: 1}).toArray # returns data in an arrayExtra: To print the data in shell while looping over it, db.students.find().forEach((stu) => {printjson(stu)})
We can find data specific to our requirement using a (.) operator in our document, like:
db.students.find({"profilePic.medium": { $gt: 90 }})We can do this: ✅
db.students.findOne({ name: "Mark" }).lastLoginBut cannot do this: ❌
db.students.find({ name: "Mark" }).lastLoginSure, we can definitely add more keys to this later, but we need to have a basic defining structure for us to avoid any reassigning of key names and such conflicts.
We make relations by passing _id of other objects or secondary collections (or tables) to our primary collection.
- One to One
- One to Many
- Many to Many