I am trying to search for records that correspond to a specific URL but want to return a customized object instead.
Here is the model I am working with:
const ReactionSchema = mongoose.Schema({
url: {
type: String,
required: true
},
emoji: {
type: String,
enum: ['happy', 'sad', 'angry'],
required: true
},
ip: {
type: String
}
})
What I want to achieve is to query the model by matching the url
and return a response structured like this:
[
{
"emoji": "happy",
"count": 4,
"reacted": true
},
{
"emoji": "sad",
"count": 9,
"reacted": false
},
{
"emoji": "angry",
"count": 7,
"reacted": false
}
]
I need to dynamically determine if reacted
is true or false by comparing the recorded ip
with a variable I have in the process.
One approach I tried involved using:
.aggregate([
{$match: {url}},
{$group: {
_id: '$emoji',
count: {$sum: 1}
}}
])
However, I am facing difficulties in combining the IPs and checking if my variable IP is present in that array or not.
Any assistance would be greatly appreciated. This is my first time seeking help!