【发布时间】:2015-09-17 18:15:51
【问题描述】:
这是我的代码:
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'mcfly');
db.once('open', function() {
var schema = new mongoose.Schema({
firstname: String,
lastname: String
});
var col = db.model('mathpeep', schema);
col.find({firstname: 'hey'}, function(err, Col) {
console.log(Col);
});
})
我的 mongod 运行正常,我运行了“mango”并在集合“mathpeep”内的数据库“mcfly”中输入了几个条目。这段代码基本上是从它工作的另一个地方复制的,一直到变量名,所以我不知道出了什么问题。
问题是我的数据库看起来像这样:
> use mcfly
switched to db mcfly
> db.createCollection('mathpeep')
{ "ok" : 1 }
> show collections
mathpeep
system.indexes
> db.mathpeep.insert({firstname: 'hey', lastname: 'ayy'})
WriteResult({ "nInserted" : 1 })
> db.mathpeep.insert({firstname: 'aaa', lastname: 'aaaaa'})
WriteResult({ "nInserted" : 1 })
> db.mathpeep.find()
{ "_id" : ObjectId("5592e96566cf1404577ebe1b"), "firstname" : "hey", "lastname" : "ayy" }
{ "_id" : ObjectId("5592e97b66cf1404577ebe1c"), "firstname" : "aaa", "lastname" : "aaaaa" }
>
然而,上面的代码返回一个空文件 (?)。当我尝试使用该架构保存文件时也会发生同样的情况:脚本完成没有问题,但是当我使用 shell 检查数据库时,没有保存任何内容。我正在从其他地方复制代码,可以很好地保存和读取数据库。我还再次运行了 mongoose 安装,如果它没有正确安装,那么我不知道如何修复它。
没有错误:当我运行脚本时,它会打印:
[]
这就是发生的一切。
我只能假设我正在连接其他地方或其他东西,因此是问题名称。
谢谢
【问题讨论】:
-
编辑了问题名称,以便对像我这样不幸的人更有用