【问题标题】:using mongodb with node js to make a chat system with history使用 mongodb 和 node js 制作一个有历史的聊天系统
【发布时间】:2014-12-04 14:10:47
【问题描述】:

尝试与历史聊天,我在使用 mongodb 和 node js 时遇到了一些问题 要清楚: 我可以在 mongodb 集合中保存一条新消息 部分代码:

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

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


    collection.insert({message:mess}, function(err, docs) {
        console.log("//////////////\r\n mess insertion :"+mess);
        collection.count(function(err, count) {
            console.log(format("count = %s", count));
        });
    });

但我无法从 mongodb 中读取内容 我试过了:

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")

    // Find all records. find() returns a cursor

    // Print each row, each document has an _id field added on insert
    // to override the basic behaviour implement a primary key factory
    // that provides a 12 byte value

    collection.find().each(function(err, doc) {

    console.log(doc);
    if(doc != null) {
        console.log("Doc from Each ");
        console.dir(doc);
    }
});

但没有成功,它返回“null”作为结果,这对我来说听起来很奇怪:(

非常感谢您对此的帮助!:)

【问题讨论】:

    标签: javascript node.js mongodb chat history


    【解决方案1】:

    您正在链接到查询。这意味着您在 mongo 光标上运行 .each 循环。您要做的就是将查询传递给回调。

    collection.find({},function(err,doc){
        //do stuff with doc and err
     })
    

    【讨论】:

    • 我不知道为什么这会得到支持。如果您这样做,doc 将成为光标。 each 实际上是去这里的正确方式。
    【解决方案2】:

    我试过你的代码,它工作正常。

    doc 在最后一次调用 each 回调时为 null 是正常的,这表明光标已用尽(请参阅 docs 示例代码中的注释以获取 each) .

    我对您的代码所做的只是添加一个尾随 }); 以便它运行:

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

    哪些输出:

    Printing docs from Cursor Each
    { _id: 5480e2667baf089e0b055c7a,
      message: 'The quick brown fox jumps over the lazy dog' }
    Doc from Each 
    { _id: 5480e2667baf089e0b055c7a,
      message: 'The quick brown fox jumps over the lazy dog' }
    { _id: 5480e293744a68a90b3f44fb,
      message: 'The quick brown fox jumps over the lazy dog' }
    Doc from Each 
    { _id: 5480e293744a68a90b3f44fb,
      message: 'The quick brown fox jumps over the lazy dog' }
    null
    

    我使用 message 运行了您的 insert 代码两次以添加它显示的两个文档。

    var mess = 'The quick brown fox jumps over the lazy dog';
    MongoClient.connect('mongodb://127.0.0.1:27017/gt-chat', function(err, db) {
        if(err) throw err;
    
        var collection = db.collection('gt-chat');
    
        collection.insert({message:mess}, function(err, docs) {
            console.log("//////////////\r\n mess insertion :"+mess);
            collection.count(function(err, count) {
                console.log("count = %s", count);
            });
        });
    });
    

    【讨论】:

    • 您好,感谢您的评论,您能告诉我您使用的代码吗? :)
    • 然后如何处理光标...?因为它与 mysql 查询安静不同,所以我不确定如何从集合中列出数据...:再次感谢!
    • tested: collection.find().each(function(err, doc) { collection.find({},function(err,doc){ //用doc和err做东西 console.log (doc); }); } );这是显示:
    • 从光标每个打印文档 { db: { domain: null, _events: {}, _maxListeners: 10, databaseName: 'gt-chat', serverConfig: { domain: null, _events: {}, _maxListeners:10,_callBackStore:[Object],_commandsStore:[Object],auth:[Object],_dbStore:[Object],主机:'127.0.0.1',端口:27017,
    • @guibs 太好了。请单击答案左侧的复选标记关闭此问题。为您的后续问题创建一个新问题。
    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 2018-11-08
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2017-10-31
    • 2023-04-06
    相关资源
    最近更新 更多