使用节点多次查询mongodb

时间:2013-03-03 17:39:49

标签: c# node.js mongodb

我似乎无法找到我的问题的正确答案,我已经遍布了stackoverflow。 考虑一下C#中的示例代码,我正在尝试将其移植到nodejs + mongobd。

var messageList = new List<MessageHelper>();

foreach( MessageActivation messageToAsk in messagesToAsk )
{
    var message     = from m in dbContext.Messages where m.Id == messageToAsk.MessageId select m;
    var jokeMessage = from m in dbContext.Messages where m.Id == messageToAsk.JokeMessageId select m;
    var user        = from u in dbContext.Users where u.Id == messageToAsk.SourceUserId select u;

    var messageHelper = new MessageHelper();
        messageHelper.AskingUserId = user.ToList()[0].Id;
        messageHelper.Message = message.ToList()[0];

    messageList.Add( messageHelper );
}

return messageList;

以节点方式执行此操作的最佳方法是什么?只是一个侧节点,dbContext将查询数据库。关键是要收集所有需要的信息,打包并发送它。

由于

编辑:

这就是我试过的

// collect needed info to make next queries in db
var messageIdList = new Array();
var jokeMessageIdList = new Array();
var sourceUserIdList  = new Array();
for( var i=0; i < messagesToAsk.length; i++ )
{
    messageIdList.push( messagesToAsk[i].MessageId      );
    jokeMessageIdList.push( jokeMessageId[i].JokeMessageId  );
    sourceUserIdList .push( jokeMessageId[i].SourceUserId   );
}

// make requests to have all the data in place
var messages = App.DataModels.Messages.find( {} );
    messages.where( 'MessageId' ).in( messageIdList );
    messages.exec( function ( err, foundMessages ) 
    {
        var jokeMessages = App.DataModels.Messages.find( {} );
            jokeMessages.where( 'JokeMessageId' ).in( jokeMessageIdList );
            jokeMessages.exec( function ( err, foundJokeMessages ) 
            {
                var users = App.DataModels.Messages.find( {} );
                    users.where( 'SourceUserId' ).in( sourceUserIdList );
                    users.exec( function ( err, foundUsers ) 
                    {
                        var messageList = new Array(); // new List<MessageHelper>();

                        for( var i=0; i < messagesToAsk.length; i++ )
                        {
                            var message     = null;
                            var jokeMessage = null;
                            var user        = null;

                            // get the data
                            for( var j = 0; j < messages.length; j++ )
                            {
                                if( messages[j].MessageId === messagesToAsk[i].MessageId )
                                {
                                    message = messages[j];
                                    break;
                                }
                            }
                            for( var k = 0; k < jokeMessages.length; k++ )
                            {
                                if( jokeMessages[k].JokeMessageId === messagesToAsk[k].JokeMessageId )
                                {
                                    jokeMessage = jokeMessage[k];
                                    break;
                                }
                            }
                            for( var l = 0; l < users.length; l++ )
                            {
                                if ( users[l].SourceUserId === messagesToAsk[l].SourceUserId )
                                {
                                    user = users[l];
                                    break;
                                }
                            }

                            var messageHelper = 
                            {
                                "AskingUserId"  : user.Id,
                                "AskingUserPic" : user.HelperPhoto,
                                "Message"       : message,
                                "JokeMessage"   : message.Type === "1" ? jokeMessage.Content
                            };

                            messageList.Add( messageHelper );
                        }

                        responseDelegate( response, messageList );
                    });
            });
    });

我在这里发布了一些正在进行转变的人。 这个想法似乎是你需要在开始迭代和汇总任何最终结果之前收集和过滤所有数据。

1 个答案:

答案 0 :(得分:0)

在对NodeJS / Mongo进行C#/ Linq数据库查询类比的上下文中回答我的问题。一种“思维”方式的映射。这主要是为了理解这种转变。正确的实现方式是使用@WiredPrairie对问题的评论中建议的Promise / Deffered之类的模式(库)。

// collect needed info to make next queries in db
var messageIdList = new Array();
var jokeMessageIdList = new Array();
var sourceUserIdList  = new Array();
for( var i=0; i < messagesToAsk.length; i++ )
{
    messageIdList.push( messagesToAsk[i].MessageId      );
    jokeMessageIdList.push( jokeMessageId[i].JokeMessageId  );
    sourceUserIdList .push( jokeMessageId[i].SourceUserId   );
}

// make requests to have all the data in place
var messages = App.DataModels.Messages.find( {} );
    messages.where( 'MessageId' ).in( messageIdList );
    messages.exec( function ( err, foundMessages ) 
    {
        var jokeMessages = App.DataModels.Messages.find( {} );
            jokeMessages.where( 'JokeMessageId' ).in( jokeMessageIdList );
            jokeMessages.exec( function ( err, foundJokeMessages ) 
            {
                var users = App.DataModels.Messages.find( {} );
                    users.where( 'SourceUserId' ).in( sourceUserIdList );
                    users.exec( function ( err, foundUsers ) 
                    {
                        var messageList = new Array(); // new List<MessageHelper>();

                        for( var i=0; i < messagesToAsk.length; i++ )
                        {
                            var message     = null;
                            var jokeMessage = null;
                            var user        = null;

                            // get the data
                            for( var j = 0; j < messages.length; j++ )
                            {
                                if( messages[j].MessageId === messagesToAsk[i].MessageId )
                                {
                                    message = messages[j];
                                    break;
                                }
                            }
                            for( var k = 0; k < jokeMessages.length; k++ )
                            {
                                if( jokeMessages[k].JokeMessageId === messagesToAsk[k].JokeMessageId )
                                {
                                    jokeMessage = jokeMessage[k];
                                    break;
                                }
                            }
                            for( var l = 0; l < users.length; l++ )
                            {
                                if ( users[l].SourceUserId === messagesToAsk[l].SourceUserId )
                                {
                                    user = users[l];
                                    break;
                                }
                            }

                            var messageHelper = 
                            {
                                "AskingUserId"  : user.Id,
                                "AskingUserPic" : user.HelperPhoto,
                                "Message"       : message,
                                "JokeMessage"   : message.Type === "1" ? jokeMessage.Content
                            };

                            messageList.Add( messageHelper );
                        }

                        responseDelegate( response, messageList );
                    });
            });
    });