Home > Enterprise >  How to find the product by id from the array of products object which is inside another array of obj
How to find the product by id from the array of products object which is inside another array of obj

Time:11-24

I have following data in my mongoDb database and I want to find the student from the array of students objects by iterating on all my students field of my database with the id the students array objects have.

I want to do it with the help of mongoose please help me find the solution as I am unable to find it.

Here is my database:

[
{
    "_id": "637ddf6d68a8284187a4d7e7",
    "college": "MIT"
    "students": [
        {
            "name": "Morgan Freeman",
            "image": "/public/morg.jpg",
            "_id": "637ddf6d68a8284187a4d7e8"
        },
        {
            "name": "John Smith",
        "image": "/public/john.jpg",
            "_id": "637ddf6d68a8284187a4d7e9"
        }
    ]
},

{
    "_id": "637ddf6d68a8284187a4dfhd",
    "college": "DOT"
    "students": [
        {
            "name": "Windy rona",
            "image": "/public/windy.jpg",
            "_id": "637ddf6d68a8284187a4dvh3"
        },
        {
            "name": "Richard",
        "image": "/public/richard.jpg",
            "_id": "637ddf6d68a8284187a4duhd"
        }
    ]
},

]

I tried using:

 database.find({}).select('students').where('_id':req.params.id)

but it didnot work

CodePudding user response:

Well if I understand your question you want to filter the result of the student array that match with the _id of the student that it providing

an aggregation with filter the project must help you.

here an example:

database.aggregate([
  {
    "$match": {
      "students._id": "637ddf6d68a8284187a4dvh3"
    },
    
  },
  {
    $project: {
      students: {
        $filter: {
          input: "$students",
          as: "student",
          cond: {
            $eq: [
              "$$student._id",
              "637ddf6d68a8284187a4dvh3"
            ]
          }
        }
      }
    }
  },
  {
    "$unwind": "$students"
  }
])

this will give you the following result:

[
  {
    "_id": "637ddf6d68a8284187a4dfhd",
    "students": 
      {
        "_id": "637ddf6d68a8284187a4dvh3",
        "image": "/public/windy.jpg",
        "name": "Windy rona"
      }
  }
]

CodePudding user response:

Option 1) Aggregation/$filter mongodb 3.2 Maybe something like this:

db.collection.aggregate([
{
 $match: {
  "students._id": "637ddf6d68a8284187a4duhd"
}
},
{
"$addFields": {
  "students": {
    "$filter": {
      "input": "$students",
      "as": "s",
      "cond": {
        "$eq": [
          "$$s._id",
          "637ddf6d68a8284187a4duhd"
        ]
      }
    }
  }
  }
 },
 {
   $unwind: "$students"
 },
{
"$project": {
  name: "$students.name",
  image: "$students.image",
  _id: "$students._id"
 }
 }
 ])

Exaplained:

  1. Match the students._id document ( good to index this field )
  2. $filter only students with the matched _id
  3. $unwind the students array
  4. project only the neccesary fields

Playground

Option 2) Find ( find first matching in a document , it will not show if there is other matching with this _id in the same document ):

db.collection.find({
  "students._id": "637ddf6d68a8284187a4duhd"
},
{
  "students": {
    "$elemMatch": {
      _id: "637ddf6d68a8284187a4duhd"
    }
  }
})

Explained:

  1. Match documents if any _id
  2. Project only the matching subelement

Playground 2

  • Related