#19 Querying Multiple documents using lookup | Relations in MongoDB | Complete MongoDB Course

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 16

  • @mwebazenicholas7075
    @mwebazenicholas7075 8 місяців тому

    I have been doing this in a very hard way 😂😂.
    Now I can do data normalisation in mongodb easily.
    Thank you 👏👏

  • @Fukara963
    @Fukara963 6 місяців тому

    Thank you so much, you saved me so much time for my project

  • @mahendranath2504
    @mahendranath2504 9 місяців тому

    Wow, Thank you so much for sharing the knowledge , liked and subscribed

  • @aishwaryakamble4604
    @aishwaryakamble4604 Рік тому

    Thank you so much... It helped me 🍀

  • @mohammadrayhanhosain8116
    @mohammadrayhanhosain8116 Рік тому

    what does the first "address" indicate in lookup? does it point out to a collection called "address"?

  • @groovebird812
    @groovebird812 Рік тому +1

    What is the advantage over a normal relational databae like mysql?

    • @AndreTannus
      @AndreTannus Рік тому +5

      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.

  • @giorgiobrugnone6879
    @giorgiobrugnone6879 Рік тому

    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

  • @aurobindobhuyan2107
    @aurobindobhuyan2107 18 днів тому

    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.

  • @mlungisidube3136
    @mlungisidube3136 Рік тому

    what if you store you address as string instead of the objectId?

  • @5u3r73
    @5u3r73 Рік тому

    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!

    • @greggord-hume3979
      @greggord-hume3979 Рік тому +2

      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.

  • @greggord-hume3979
    @greggord-hume3979 Рік тому +1

    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
    }

  • @mostbeautifulpsychopath3876

    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

  • @Pareshbpatel
    @Pareshbpatel Рік тому

    {2023-08-23}

  • @anavera9175
    @anavera9175 Рік тому

    Why do I get "Aggregate has empty pipeline" ?
    const comments = await CommentModel.aggregate({
    $lookup: {
    from: "users",
    localField: "user_id",
    foreignField: "_id",
    as: "user"
    }
    })