【发布时间】: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