There really isn't one. I come from a relational db background and have been using Mongo for about a year. The only thing Mongo has going for it is that you don't have to think about your db structure before you start prototyping your app, you can just shove data in and not worry about it. That said, once your application becomes even mildly complex, this "advantage" turns into nightmares and sleepless nights. For instance, when you embed documents, how do you keep consistency across collections? When the customer's name or address or phone number changes, you have to go into every single place where it's embedded and update that. That is why I'm watching this video a year into using Mongo. And all I can find are very basic demos (like this one) of embedding vs referencing, all of which never go into integrity or consistency. Ask yourself: Why does a non-relational database need a lookup operation? The answer is in the lookup syntax: it's a JOIN. There is no such thing as a non-relational database. :D Mongo might be interesting to use when: a) your data *is unstructured* or *structure is not important*; b) You *never ever* update data, you just collect it; c) when you're prototyping and want to break ground quickly; But IMHO, take your time, think about your db structure, and use an RDBMS.
Great video tnx. Is it possible to query results by lookup field? In your example is it possible to get all and only Customers from Germany? Thank you for reply
I want to implement a global search functionality in Node.js. Can someone guide me about this? I have 5 different models and want to add search functionality in deeply nested objects.
Hi, thanks for this tutorial :D One question, what about a reference to an array of other documents within the second object? for instance: -> user ----> role ObjectId(''xxxxxx) --------> permission 1 ObjectId(''aaaaaa") --------> permission 2 ObjectId(''bbbbbb") Hope you can help me Thanks!
Im sure you have found the answer by now. In this case you can do another lookup in the pipeline, after the first lookup. $lookup: { from: "another_collection", localField: "addr._Id", foreignField: "id", as: "another_collection" } ^ in this case mongo will automatically loop through all objects in one collection and matching them in another. Dont forget to use $unwind in one of my above comments if you want the result as an object instead of an array of objects.
If you want address to be an object instead of an array of objects you use unwind after the lookup in the pipeline array: $unwind: { path: "$addr", preserveNullAndEmptyArrays: false }
but why when i see in my mongodb compass i don't see the relation i have two collection user and school i want to join them and i did in mongosh but in compass i dont see any diiference
I have been doing this in a very hard way 😂😂.
Now I can do data normalisation in mongodb easily.
Thank you 👏👏
Thank you so much, you saved me so much time for my project
Wow, Thank you so much for sharing the knowledge , liked and subscribed
Thank you so much... It helped me 🍀
what does the first "address" indicate in lookup? does it point out to a collection called "address"?
What is the advantage over a normal relational databae like mysql?
There really isn't one. I come from a relational db background and have been using Mongo for about a year. The only thing Mongo has going for it is that you don't have to think about your db structure before you start prototyping your app, you can just shove data in and not worry about it. That said, once your application becomes even mildly complex, this "advantage" turns into nightmares and sleepless nights. For instance, when you embed documents, how do you keep consistency across collections? When the customer's name or address or phone number changes, you have to go into every single place where it's embedded and update that.
That is why I'm watching this video a year into using Mongo. And all I can find are very basic demos (like this one) of embedding vs referencing, all of which never go into integrity or consistency.
Ask yourself: Why does a non-relational database need a lookup operation? The answer is in the lookup syntax: it's a JOIN. There is no such thing as a non-relational database. :D
Mongo might be interesting to use when: a) your data *is unstructured* or *structure is not important*; b) You *never ever* update data, you just collect it; c) when you're prototyping and want to break ground quickly;
But IMHO, take your time, think about your db structure, and use an RDBMS.
Great video tnx.
Is it possible to query results by lookup field?
In your example is it possible to get all and only Customers from Germany?
Thank you for reply
I want to implement a global search functionality in Node.js. Can someone guide me about this?
I have 5 different models and want to add search functionality in deeply nested objects.
what if you store you address as string instead of the objectId?
Hi, thanks for this tutorial :D
One question, what about a reference to an array of other documents within the second object?
for instance:
-> user
----> role ObjectId(''xxxxxx)
--------> permission 1 ObjectId(''aaaaaa")
--------> permission 2 ObjectId(''bbbbbb")
Hope you can help me
Thanks!
Im sure you have found the answer by now. In this case you can do another lookup in the pipeline, after the first lookup.
$lookup: {
from: "another_collection",
localField: "addr._Id",
foreignField: "id",
as: "another_collection"
}
^ in this case mongo will automatically loop through all objects in one collection and matching them in another.
Dont forget to use $unwind in one of my above comments if you want the result as an object instead of an array of objects.
If you want address to be an object instead of an array of objects you use unwind after the lookup in the pipeline array:
$unwind: {
path: "$addr",
preserveNullAndEmptyArrays: false
}
but why when i see in my mongodb compass i don't see the relation i have two collection user and school i want to join them and i did in mongosh but in compass i dont see any diiference
{2023-08-23}
Why do I get "Aggregate has empty pipeline" ?
const comments = await CommentModel.aggregate({
$lookup: {
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user"
}
})