Node.js Couchdb检索两个用户之间的聊天历史记录

Node.js Couchdb检索两个用户之间的聊天历史记录,node.js,nosql,couchdb,cradle,Node.js,Nosql,Couchdb,Cradle,我目前正在使用Couchdb保存两个用户之间的聊天历史记录。My db包含每个用户输入的每个插入消息。它们是这样输入的: { _id: (timestamp), from: sender, to: receiver, message: msg } function(doc) { if (doc.from && doc.to){ (doc._id, doc); } } function(doc) { va

我目前正在使用Couchdb保存两个用户之间的聊天历史记录。My db包含每个用户输入的每个插入消息。它们是这样输入的:

{
  _id: (timestamp),
  from: sender,
  to: receiver,
  message: msg
}
   function(doc) {
     if (doc.from && doc.to){
         (doc._id, doc);
     }
   }
function(doc) {
    var key = [doc.from, doc.to].sort(function (a, b) {
        return a.localeCompare(b);
    });

    emit(key.concat(doc._id), doc);
}
我的视图编码如下:

{
  _id: (timestamp),
  from: sender,
  to: receiver,
  message: msg
}
   function(doc) {
     if (doc.from && doc.to){
         (doc._id, doc);
     }
   }
function(doc) {
    var key = [doc.from, doc.to].sort(function (a, b) {
        return a.localeCompare(b);
    });

    emit(key.concat(doc._id), doc);
}
我很难想出一个办法,根据我当前的设置检索两个用户之间的聊天转换

我在view函数中使用了以下方法:

res.forEach(function (row) {
    if(row.from != null || row.to != null){
          if((row.from == socket.username && row.to == receiver) || (row.from == receiver && row.to == socket.username)){
             MsgHistoryContent += row.message + '\n';
          }
    }
});
然而,这并没有检索到两个用户之间的所有对话,因为它检索到了每个用户的所有消息

谁能给我一个线索?任何帮助都将不胜感激


附言:我正在使用node.js和bradle。

您可以创建如下映射函数:

{
  _id: (timestamp),
  from: sender,
  to: receiver,
  message: msg
}
   function(doc) {
     if (doc.from && doc.to){
         (doc._id, doc);
     }
   }
function(doc) {
    var key = [doc.from, doc.to].sort(function (a, b) {
        return a.localeCompare(b);
    });

    emit(key.concat(doc._id), doc);
}

然后,您将能够通过查询“?startkey=[“user1”,“user2”]&endkey=[“user1”,“user2”,“{}]”来获取用户之间的对话。当然,开始键和结束键0和1的索引都应该是字母排序的。

在发送到客户端之前,您可以使用_list函数进行筛选,尽管您仍然需要循环所有用户的每个聊天。您可以输出每个用户的视图,然后自己在客户端整理它们。太快了!它实际上不起作用:(!!可能是我在视图上放置startkey和endkey的方式。我如何使用摇篮传递它们?我是这样做的:couchdb.view('chat/history',filterge,function(err,results){results.forEach(function(row){console.log(row.message+'\n');});});它给了我“无法调用未定义的方法'forEach'。通过大量搜索和使用您的回复,我终于能够解决这个问题。谢谢:)!!