J
J
js-person2021-07-16 00:19:24
JavaScript
js-person, 2021-07-16 00:19:24

How to get an object from an array of objects by id in mongodb?

In my mongo database, data is stored in this form

[
    {
        "product_id": [],
        "_id": "60eebb77c8c95045c4231d50",
        "name": "phone",
        "__v": 0
    },
    {
        "product_id": [],
        "_id": "60eebb95c8c95045c4231d52",
        "name": "notebook",
        "__v": 0
    }
]

i need to get object by id :'60eebb95c8c95045c4231d52'
result:
{
        "product_id": [],
        "_id": "60eebb95c8c95045c4231d52",
        "name": "notebook",
        "__v": 0
}

I found an option that it can be done through Objectid, but I couldn’t apply how

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
podnebes_m, 2021-07-16
@podnebes_m

let result = arr.find(x => x._id === '60eebb95c8c95045c4231d52');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question