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 = [{}, {}, {}]
3
Answers
you can refer to this projection document MongoDB projection
if you don’t want id also then
Here
1
means that you want that field in the response and0
means you want to avoid that field in responseawait places.findOne({name: placename}).project({hotels: 1, _id: 0})
You can simply use the select property to show only required fields.If you want to include _id then you remove _id property from select else it is added in projection by default.