[英]Mongoose push - find or update to array of objects
My situation is as following: 我的情况如下:
I have a session
collection, each session is a chatroom. 我有一个
session
集合,每个会话都是一个聊天室。
When a user sends a new message, other users should be notified, but to prevent spam, I need to build a check if a user got a notification mail in the last 10 minutes. 当用户发送新邮件时,应该通知其他用户,但是为了防止垃圾邮件,我需要检查用户是否在最近10分钟内收到了通知邮件。
So my session collection has a notification
array of objects with: sessionId
, userId
and date
所以我的会话集合具有对象的
notification
数组,其中包括: sessionId
, userId
和date
Everytime someone sends a new message, I have to send the mails and push to the notifications
column, but it should overwrite the userId
to prevent the notifications column from growing. 每当有人发送新消息时,我都必须发送邮件并推送到
notifications
列,但是它应该覆盖userId
以防止通知列增长。
This is what I've tried: 这是我尝试过的:
sessionSchema .findByIdAndUpdate(sessionId, { $push: { notifications: { userId: userId, date: moment().utc().toDate() } } }, callback)
However with push, the column keeps growing. 但是,随着推动,色谱柱不断增长。 How can I use push to overwrite the already created notifications by a certain userId?
如何使用push覆盖某个用户ID的已创建通知?
Maybe, try to add options in query to DB. 也许,尝试向数据库添加查询中的选项。
sessionSchema
.findByIdAndUpdate(sessionId, {
$push: {
notifications: {
userId: userId,
date: moment().utc().toDate()
}
}
}, { new: true }, callback)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.