I'm looking to query the item data from my mongoose data collection. I only need the "stocked" boolean value for the user "456" item "4", but when I query in my example, I instead receive the entire user object.
Data:
data = [{
userId: "123",
items: [{
item: "2",
stocked: false
},
{
item: "3",
stocked: true
},
{
...more items
}
],
},
{
userId: "456",
items: [{
item: "1",
stocked: true
},
{
item: "4",
stocked: true
},
{
...more items
}
],
},
{
...more users
}
]
Route:
router.post("/example", (req, res) => {
Data.findOne({
userId: "456",
"items.item": "4"
}
}).then((item) => {
console.log(item) // desired: {item: "4", stocked: true}
if (item.stocked) {
console.log("Item is stocked!")
}
})
})
Problem: The response is the entire user object including all items:
{
userId: "456",
items: [{
item: "1",
stocked: true
},
{
item: "4",
stocked: true
},
{
...more items
}
],
},
Desired response: { item: "4", stocked: true }
Any tips or help would be appreciated!