gpt4 book ai didi

c# - 使用node对mongodb进行多次查询

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:18 25 4
gpt4 key购买 nike

我似乎找不到我的问题的正确答案,我已经浏览了 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;

以 Node 的方式做到这一点的最佳方法是什么?只是一个侧 Node ,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 );
});
});
});

我把它贴在这里是为了其他正在轮类的人。这个想法似乎是,您需要先收集并过滤所有数据,然后再开始迭代和组装任何最终结果以发送。

最佳答案

在类比 NodeJS/Mongo 的 C#/Linq DB 查询的背景下回答我的问题。一种“思维”方式的映射。这主要是为了理解这种转变。正确的实现是使用像 Promise/Deffered 这样的模式(库),如 @WiredPrairie 对问题的评论中所建议的。

// 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 );
});
});
});

关于c# - 使用node对mongodb进行多次查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188584/

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