我知道如何使用,Model.findOne
但我希望将Mongoose 查询用于同一任务。
如何使用 Mongoose 查询来做到这一点?
所以我有这个旧代码:
Notification.find(
{
subscribers: user.id
}
)
...
我正在研究这个替代代码:
const r = await Notification.find()
.where("subscribers")
.equals(user.id)
...
但我认为不适.equals
用于这种情况,因为它subscribers
是一个ObjectId
s数组并且user.id
是一个ObjectId
基于字符串的字符串。
我搜索了官方 API 文档并没有找到答案。没有错误消息。
我找到了Query.prototype.all
方法。现在代码如下所示:
const r = await Notification.find().where("subscribers").all([user.id]);
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句