How to get data of inside the array of objects in mongo db

I am working in a MERN project and i want to fetch data from inside the array of objects through the inside objectid
My model view


I want to fetch name:"vikash" and attendance:"present" through the objectid:"63cbb3b15cd59c7810ab89a2

for this i had tried two code

app.get('/stud/:id', (req, res) => {
    ProjectSchema.find({"projectmembers._id":req.params._id}, (err, data) => {
        if (err) return res.status(500).send(err);
        {$match : {
            "projectmembers._id" : req.params._id
    ], (err, data) => {
        if (err) return res.status(500).send(err);

Output of both code is []

“_id” field’s type is “ObjectId” whereas “req.params._id” is transferred as a string in the request. try one of these, or check documentation (for mongoose?) how you do that:

  • "projectmembers._id": ObjectId(req.params._id)
  • "projectmembers._id":{"$oid": req.params._id}

after trying one of these…output is

    "stringValue": "\"{ 'projectmembers._id': { '$oid': undefined } }\"",
    "valueType": "Object",
    "kind": "ObjectId",
    "value": {
        "projectmembers._id": {}
    "path": "_id",
    "reason": {},
    "name": "CastError",
    "message": "Cast to ObjectId failed for value \"{ 'projectmembers._id': { '$oid': undefined } }\" (type Object) at path \"_id\" for model \"Project\""

The undefined in the error above means that you do not pass a valid value. It looks like a bug in your code. You defined your route as

but you access the route’s id parameter as

I don’t do React but I suspect that it should be

1 Like

@steevej , i missed that :stuck_out_tongue_winking_eye: both ObjectId() and $oid are doomed to fail here as they are passed an “undefined” value.

req.params and req.body are general names but mostly point to an “express.js” server for javascript. As steve noted, you have to use the exact name you gave in your resources; here you have /stud/:id so it has to be

by the way, if you use mongoose, you may not need to convert these manually. check the use of a model schema for passing id around.

1 Like