MongoDB嵌套查询仅返回最后一次出现的结果 [英] MongoDB Nested Query Returns Only Last Occuring Result

查看:191
本文介绍了MongoDB嵌套查询仅返回最后一次出现的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这段代码包含一个for循环内的嵌套查询

Okay, I have this code containing a nested query inside a for loop

var query = records.find({$or:[{starter:data},{receiver:data}]},{});//check the records table for all persons the logged in user has spoken to
    query.sort('-createDate').exec(function (err, docs){
        if(err) throw err;

        for(var i=docs.length-1; i>= 0; i--)
        {

           var starter  = docs[i].starter;
            var receiver = docs[i].receiver;
            var lasttxt = docs[i].lastMessage; 

            if (starter == socket.usernames){
              var target = receiver;
            }else
            {
              var target = starter;
            }

          usersrec.find({username:target},{}).lean().exec(function (errx, docx){
                if(errx) throw errx;

                docx[0].message = lasttxt;
                socket.emit('usernames', docx);
          });
        }
    })

它旨在获取当前登录用户所交谈的每个人的最新消息,并将其存储在lasttxt变量中. 问题是它仅获得数据库中最后一个用户的最后一条消息 然后,它将最后一条消息分配给每个人,作为他们自己的最后一条消息.

Its meant to get the last message of each person the currently logged in user has spoken to and store in the lasttxt variable. Problem is it only gets the last message of the last user in the database It then then assigns this last message to everyone as their own last msg.

这不会影响数据库的记录.只是客户端 我想念什么?

推荐答案

要浏览js异步代码,我使用socket.io来回进行了一些工作,并且奏效了

To navigate the js async, I did some to and fro emitting with socket.io and it worked

在服务器端

var query = records.find({$or:[{starter:data},{receiver:data}]},{});//check the records table for all persons the logged in user has spoken to
query.sort('-createDate').exec(function (err, docs){
    if(err) throw err;

    for(var i=docs.length-1; i>= 0; i--)
    {

       var starter  = docs[i].starter;
        var receiver = docs[i].receiver;
        var lasttxt = docs[i].lastMessage; 

        if (starter == socket.usernames){
          var target = receiver;
        }else
        {
          var target = starter;
        }

      var userlast = target+" "+lasttxt;
                socket.emit('lastly', userlast);//Emit the username and last message for the client to emit back here
    }
})

在您的客户端,拾取发出的数据

 socket.on('lastly', function(data){//Recieve the data and send right back
                  socket.emit('lastly2', data);
              });

回到服务器端,拾取发送回的数据

Back on you server side, pick up the data sent back

socket.on('lastly2', function(data){//receive the username and last message to work with

var check = data;
var space = check.indexOf(' ');
var name = check.substr(0, space);
var msg = check.substr(space+1);

usersrec.find({username:name},{}).lean().exec(function (errx, docx){
            if(errx) throw errx;

            docx[0].message = msg;
            socket.emit('usernames', docx);
      });

是的,这可能是非常规的,但至少可以完成工作.我愿意接受更好的建议

Yeah its probably unorthodox, but at least it gets the job done. Im open to better suggestion tho

这篇关于MongoDB嵌套查询仅返回最后一次出现的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆