Trying to find the data using a condition which is not on root level

Hello,
I have some data in DB like this:
{

        "_id": "63b14c1852f8a7e6c15efd06",
        "productName": "xxxxxxxxxx",
        "playerID": "xxxxxxxxxxxxx",
        "requestEmail": "test18@gmail.com",
        "timestampDetails": {
            "dayName": "Sun",
            "day": "01",
            "monthName": "Jan",
            "year": "2023"
        },
        "date": "Sun, 01 Jan 2023 09:02:14 GMT",
        "orderId": 9,
        "paid": true,
        "status": "পরিশোধ করা হয়েছে",
        "status": "Paid",
        "createdAt": "2023-01-01T09:02:16.265Z",
        "updatedAt": "2023-01-01T09:04:58.334Z",
        "__v": 0
    },

now I need to find data using month name and year under timestampDetails.
I tried to find those data like this:
<> const result = await OrderSchema.aggregate({
timestampDetails: {
monthName: “Jan”,
year: “2023”,}
,status: “Paid”, }).sort({createdAt: -1,}); </>

but it doesn’t work. I’m not expert in mongodb/mongosse. Could you someone help me?

When you write

you ask for the field timestampDetails to be equal to the object

{
    monthName: "Jan",
    year: "2023" }

In, JS and MongoDB, objects are equals when they have the same fields and the same values in the same order. In your timestampDetails you have extra fields that are not specified in your query so the objects are not equal. To do what you want you need to use the dot notation to specify a field to field query such as:

"timestampDetails.monthName" : "Jan" ,
"timestampDetails.year" : "2023" ,
"status" , "Paid"
2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.