Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Node.js 尝试从数据库集合(mongodb)获取消息历史记录_Node.js_Mongodb_Chat_History - Fatal编程技术网

Node.js 尝试从数据库集合(mongodb)获取消息历史记录

Node.js 尝试从数据库集合(mongodb)获取消息历史记录,node.js,mongodb,chat,history,Node.js,Mongodb,Chat,History,我回到了我的聊天系统历史记录中,使用mongodb,我不能让我们说关于db Collection的最后20条消息,我很确定这是一些要添加到查找例程中的过滤,但我不知道在哪里以及如何做到这一点,这是我的一段代码: MongoClient.connect('mongodb://127.0.0.1:27017/gt-chat', function(err, db) { if(err) throw err; var collection = db.collection('gt-chat'); con

我回到了我的聊天系统历史记录中,使用mongodb,我不能让我们说关于db Collection的最后20条消息,我很确定这是一些要添加到查找例程中的过滤,但我不知道在哪里以及如何做到这一点,这是我的一段代码:

MongoClient.connect('mongodb://127.0.0.1:27017/gt-chat', function(err, db) {
if(err) throw err;

var collection = db.collection('gt-chat');

console.log("******************************Printing docs from Cursor Each")

collection.find().each(function(err, doc) {
 console.log(doc);
if(doc != null) {
console.log("Doc from Each ");
console.dir(doc);
 }
});
});
此代码显示:

{id:5488473660eda4ac251d6688, 信息:“敏捷的棕色狐狸跳过了懒狗”} 我不需要_id值,我只想要最后20条消息作为结果。 希望说得够清楚,, 提前感谢您在这方面的帮助!:

您可以尝试以下方法:

MongoClient.connect('mongodb://127.0.0.1:27017/gt-chat', function(err, db) {
   if(err) throw err;
   var collection = db.collection('gt-chat');
   console.log("******************************Printing docs from Cursor Each")
   collection.find({}, {_id: 0}).sort({$natural: 1}).limit(20).each(function(err, doc) {
      console.log(doc);
      if(doc != null) {
         console.log("Doc from Each ");
         console.dir(doc);
      }
   });
});
编辑

要写入浏览器,请使用响应对象,如下所示

require('http').createServer(function(req, res) {
    MongoClient.connect('mongodb://127.0.0.1:27017/gt-chat', function(err, db) {
       if(err) throw err;
       var collection = db.collection('gt-chat');
       console.log("******************************Printing docs from Cursor Each")
       collection.find({}, {_id: 0}).sort({$natural: 1}).limit(20).each(function(err, doc) {
          console.log(doc);
          if(doc) {
             res.writeHead(200, {"Content-Type": "text/plain"});
             res.write(JSON.stringify(doc) + "\n");
          }
          else {
             res.end();
          }
      });
    });
}).listen(port, host);

您好,这一个确实有效,谢谢,但我遇到了一个新问题,我可以在shell控制台下看到结果,但在浏览器下看不到它们。。。。有什么提示吗?谢谢这是最后使用的代码,但在浏览器:MongoClient.connect'下不会显示任何结果mongodb://127.0.0.1:27017/gt-chat',functionerr,db{ifer throw err;var history=;var collection=db.collection'gt-chat';console.log**********************************************从游标打印每个集合的文档。查找{},{u id:0}。排序{$natural:1}.limit20.eachfunctionerr,doc{console.logdoc;ifdoc!=null{console.logdoc from Each;history=history+doc+\r\n;console.dirdoc;}};};