Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 使用minimongo从多个mongo集合获取数据的更好方法_Mongodb_Meteor_Minimongo - Fatal编程技术网

Mongodb 使用minimongo从多个mongo集合获取数据的更好方法

Mongodb 使用minimongo从多个mongo集合获取数据的更好方法,mongodb,meteor,minimongo,Mongodb,Meteor,Minimongo,我目前正在Meteor中创建一个web应用程序。此应用程序使用MongoDb,当从客户端进行查询时,它使用minimongo与底层MongoDb交互 我有两个集合定义如下 const chats = { userIds: [], //other data irrelevant to question }; const users = { userId: string, username: string

我目前正在Meteor中创建一个web应用程序。此应用程序使用MongoDb,当从客户端进行查询时,它使用minimongo与底层MongoDb交互

我有两个集合定义如下

const chats = {
      userIds: [],
      //other data irrelevant to question
    };

const users = {
          userId: string,
          username: string
          //other data irrelevant to question
        };
因此,基本上,聊天室集合包含聊天室中所有用户的唯一用户id,而用户集合包含系统中的所有用户。我试图在一个聊天文档中查询所有用户的用户名

目前,我通过在一次聊天中首先查询所有用户id,然后使用javascript对这些用户id进行迭代以找到相应的用户名来实现这一点,如下所示:

var thisChat = Chats.findOne(this.chatId); //get current chat document
          var userList = thisChat.userIds; //get list of user id's from this chat      

          this.newUserList = [];
            for(var i = 0; i < userList.length; i++) { //iterate over user id's
              var tempUser = Meteor.users.find({_id: userList[i]}).fetch(); //find username for this userId
              this.newUserList.push(tempUser[0]); //add this username to an array
            }
          });
        });

    return this.newUserList; //return list of usernames
var thisChat=Chats.findOne(this.chatId)//获取当前聊天文档
var userList=thisChat.userIds//从此聊天中获取用户id列表
this.newUserList=[];
对于(var i=0;i

这种方法非常难看,所以我想知道是否有更干净的方法使用minimongo(某种连接等价物?)。我看过其他使用populate或aggregate的帖子,但它们在minimongo中不可用。

以下是我在服务器级别使用的方法

Meteor.publishTransformed('currentChat',function(){ 返回Chats.find() .serverTransform({ “用户名”:函数(聊天){ 返回Meteor.users.find({ _身份证:{ $in:chat.userIds } }).map(函数(用户){ 返回user.username; } } }); });
现在您可以从对象本身获取它

var thisChat = Chats.findOne(this.chatId);
var usernames = thisChat.usernames;

另一个流行的软件包是

如果您在客户端上有用户和聊天,那么您可以在find中使用
$in:
操作符,后跟
.map()
,以避免所有的循环和推送

const usernames = Meteor.users.find(
  { _id: { $in: Chats.findOne(this.chatId).userIds }}
  ).map(doc => doc.username);