Using Mongoose in Node.js. I have this model structure in MongoDB:
{
"_id":"5ac17dc27707e91ed00cea7d",
"appid":123,
"name":"Stellaris",
"code":"xxxxxxxxxxx"
}
I need to select all items BUT when there is more items of same appid, select just ONE or each appid. Distinct return just array of one field. How select whole objects instead where items with same appid are only once?
Try this
db.getCollection('Collection').aggregate([{
$group: {
_id: '$appid',
"docs": {
$first: {
"name": "$name",
"code": "$code"
}
}
}
}])
OR If you don't want docs field.
db.getCollection('Collection').aggregate([{
$group: {
_id: '$appid',
"name": {
$first: "$name"
},
"code": {
$first: "$code"
}
}
}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With