Search code examples
node.jsmongodbcrud

Load a single data from a document in mongoDB


How can I find only the "hotels" property? Data in MongoDB-

[
    {
        "picture": "https://d.bcb.ic/dKkqr2da/lalkhal.jpg",
        "name": "Lalakhal",
        "about": "Lalakhal is....",
        "latitude": 25.1048,
        "longitude": 92.1770,
        "hotels": [{}, {}, {}]
    },
    {
        "picture": "https://d.bcb.ic/dKkqr2da/lalkhal.jpg",
        "name": "Lalakhal",
        "about": "Lalakhal is....",
        "latitude": 25.1048,
        "longitude": 92.1770,
        "hotels": [{}, {}, {}]
    },
]

Here is my code

const places = client.db("travel-guru").collection("places");
const hotels = await places.findOne({name: placename})

I don't need the all result document. I need just hotels only for a specific document. I am expecting the result- hotels = [{}, {}, {}]


Solution

  • await places.findOne({name: placename}).project({hotels: 1, _id: 0})