gpt4 book ai didi

mongodb - Meteor collection.find - 返回多个值

转载 作者:可可西里 更新时间:2023-11-01 10:43:50 25 4
gpt4 key购买 nike

我想结合 meteor-roles 包在 meteor 中实现一个通知功能,我想向角色中的所有用户显示相同的通知。我认为将主要通知存储在通知集合中并将对具有 id 和“读取”属性的通知的引用存储在用户集合中是个好主意。否则,我需要为角色中的每个用户存储每个通知。

我的数据库:

User Collection: "username": "UserXYZ", "notifications": [[{ "_id": "231", "read": "false"}], [{ "id": "3234", "read": "true"}]] …
Notification Collection: "_id": "231" …

现在我想找到相应的通知,但问题是,我无法告诉查找函数我想要显示多个通知。

我认为这样的事情可以做到:

notifications: function() {
var user = Users.findOne({_id: Meteor.userId()});
return Notifications.find({_id: user.notifications._id, read: false});
}

如有任何帮助,我们将不胜感激。

最佳答案

您需要使用聚合来执行此操作。 $unwind 操作允许您查询单个通知:

Users.aggregate(
{$match: {_id: Meteor.userId()},
{$unwind: "$notifications"},
{$match: {"notifications.read": true}}
)

这会将每个通知作为其自己的文档返回。

关于mongodb - Meteor collection.find - 返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com