Ich habe einen notification
model
mit diesem Schema
hier:Ist es möglich, nur einen von jedem notificationType/postId zu filtern und zurück zu holen?
const NotificationSchema = new Schema({
type: {
type: String,
required: true
},
createdAt: {
type: Number,
required: true
},
postId: {
type: Schema.Types.ObjectId,
ref: 'post',
required: true
},
recipients: [{
type: Schema.Types.ObjectId,
ref: 'user',
required: true
}]
});
Ich bin eine Route Controller bauen, die alle notifications
bekommen, wo sie eine der recipients
sind hier:
getNotifications(req, res, next) {
const userId = req.params.id;
Notification.find({ recipients: userId })
.sort({ createdAt: 1 })
.then(notifications => res.send(notifications))
.catch(next);
},
Ich möchte auch um sicherzustellen, dass, wenn es mehrere notifications
, die die gleichen type
haben, wie postAuthorNotification
AND die gleiche postId
, dann bringt es nur diezurückmit der neuesten createdAt
timestamp
.
Der Grund dafür ist, dass wenn der Beitrag eines Benutzers 20 Mal kommentiert wird. Es wird eine notification
für jeden geben. Ich möchte dem Benutzer nur EINE Benachrichtigung zurückschicken, die letzte.
Ist das überhaupt möglich mit Mongo/Mungo?
Vielen Dank.