I have the following document, where globalData
is an array inside of my globalEventSchema
.
{
"globalData": [
{
"event": "event-a",
"generatedId": "969046612024361060",
"_id": "626a0d5553e6eb2f5e1ce714"
},
// More instances of the Schema
],
"__v": 0
}
I want to search through my GlobalEvents
and find the globalData
array where the event
matches my given string. How can I achieve that using mongoose?
CodePudding user response:
The following will look in the globalData
array for the specified event
router.get('/', async (req, res) => {
const { event } = req.body;
const globalEvent = await GlobalEvent.findOne({
'globalData.event': event,
});
if (globalEvent) {
res.json(globalEvent);
} else {
res.json({
error: 'No global event found',
});
}
});