Siddharth
Siddharth

Reputation: 153

findOne returning full document

This is the Data stored in MongoDB database under the collection name of padlos

 {
        "_id" : ObjectId("60ffb89473dc672be32909c2"),
        "courses" : [
            {
                "_id" : ObjectId("60ffb89473dc672be32909c3"),
                "course" : "BCA",
                "semesters" : [
                    {
                        "_id" : ObjectId("60ffb89473dc672be32909c4"),
                        "sem" : 1,
                        "subjects" : [
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909c5"),
                                "subject" : "C++",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909c6"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909c7"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909c8"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909c9"),
                                "subject" : "IC & IT",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909ca"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909cb"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909cc"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "_id" : ObjectId("60ffb89473dc672be32909cd"),
                        "sem" : 2,
                        "subjects" : [
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909ce"),
                                "subject" : "Java",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909cf"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d0"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d1"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909d2"),
                                "subject" : "SQL",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909d3"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d4"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d5"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        "__v" : 0
    }

This is the code that i am using to query and fetch data

app.get('/',function(req, res)
{
    PadhloSchema.findOne(
        {"courses.course" : "BCA" , "courses.semesters.sem" : 1, "courses.semesters.subjects.subject" :"C++"},
    function(err, courses){
        if(!err)
        {
            res.send(courses);
        }
        else
        {
            res.send(err);
        }
    })
});

I only want to fetch data where the course is BCA ,the semester(sem) is 1 and the subject is C++ and the data in the response must look like this :

{
    "_id" : ObjectId("60ff08a977ec48b84ec07b46"),
    "subject" : "C++",
    "units" : [
                {
                  "_id" : ObjectId("60ff08a977ec48b84ec07b47"),
                  "unit" : 1,
                  "topics" : [
                              {
                                "_id" : ObjectId("60ff08a977ec48b84ec07b48"),
                                "topic" : "Basics"
                              },
                              {
                                "_id" : ObjectId("60ff08a977ec48b84ec07b49"),
                                "topic" : "DataTypes"
                              }
                            ]
                }
              ]
}

But rather i am all the data back in the response:

 {
        "_id" : ObjectId("60ffb89473dc672be32909c2"),
        "courses" : [
            {
                "_id" : ObjectId("60ffb89473dc672be32909c3"),
                "course" : "BCA",
                "semesters" : [
                    {
                        "_id" : ObjectId("60ffb89473dc672be32909c4"),
                        "sem" : 1,
                        "subjects" : [
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909c5"),
                                "subject" : "C++",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909c6"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909c7"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909c8"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909c9"),
                                "subject" : "IC & IT",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909ca"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909cb"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909cc"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "_id" : ObjectId("60ffb89473dc672be32909cd"),
                        "sem" : 2,
                        "subjects" : [
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909ce"),
                                "subject" : "Java",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909cf"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d0"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d1"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909d2"),
                                "subject" : "SQL",
                                "units" : [
                                    {
                                        "_id" : ObjectId("60ffb89473dc672be32909d3"),
                                        "unit" : 1,
                                        "topics" : [
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d4"),
                                                "topic" : "Basics"
                                            },
                                            {
                                                "_id" : ObjectId("60ffb89473dc672be32909d5"),
                                                "topic" : "DataTypes"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        "__v" : 0
    } 

Please help and tell me where i am doing wrong.I am new and noob in mongoDB. I am using the latest MongoDB version 5.0.1

UPDATED ANSWER after Comment by Rahul Soni

app.get('/',function(req, res)
{
    PadhloSchema.find(
        {
            courses : {course : "BCA",semesters : {sem : 1 ,subjects :{subject : "C++"}}}
      
        },
               function(err, courses){
        if(!err)
        {
            res.send(courses);
        }
        else
        {
            res.send(err);
        }
    })
});

But now it is giving me null array

[]

Upvotes: 0

Views: 113

Answers (1)

Rahul Soni
Rahul Soni

Reputation: 4968

MongoDB returns data at the document level. https://docs.mongodb.com/manual/tutorial/query-array-of-documents/

Basically, it is working as expected. You are searching for the document and you are getting precisely that. If you are looking for a subdocument, you will need to unwind it like so:

db.tmp.aggregate({$unwind:"$courses"},{$unwind:"$courses.semesters"},{$unwind:"$courses.semesters.subjects"},{$match:{"courses.course":"BCA", "courses.semesters.sem": 1, "courses.semesters.subjects.subject" :"C++"}}).pretty()

NB: I have simply added your document to a database and showing it at a the mongo level. Node.js would work similarly.

{
    "_id" : ObjectId("60ffd533bccc96b9985944a9"),
    "courses" : {
        "_id" : ObjectId("60ffb89473dc672be32909c3"),
        "course" : "BCA",
        "semesters" : {
            "_id" : ObjectId("60ffb89473dc672be32909c4"),
            "sem" : 1,
            "subjects" : {
                "_id" : ObjectId("60ffb89473dc672be32909c5"),
                "subject" : "C++",
                "units" : [
                    {
                        "_id" : ObjectId("60ffb89473dc672be32909c6"),
                        "unit" : 1,
                        "topics" : [
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909c7"),
                                "topic" : "Basics"
                            },
                            {
                                "_id" : ObjectId("60ffb89473dc672be32909c8"),
                                "topic" : "DataTypes"
                            }
                        ]
                    }
                ]
            }
        }
    }
}

Upvotes: 1

Related Questions