mongoose不认可收集的名字
这是我的代码:
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运行正常,我跑了“芒果”,并input了一些条目到“mathpeep”集合内的数据库“mcfly”。 这段代码基本上是从另外一个地方复制到variables名,所以我不知道什么是错的。
问题是我的数据库看起来像这样:
> 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安装,如果它没有正确安装,那么我不知道如何解决它。
没有错误:当我运行脚本,它打印这个:
[]
而这就是发生的一切。
我只能假设我连接到别的什么地方,所以问题的名字。
谢谢
我想这是因为mongoose增加了一个复数。 所以它正在检查收集mathpeeps
。 要明确地告诉集合名称使用第三个参数:
var col = db.model('mathpeep', schema, 'mathpeep');
为了确定它所做的查询是在你的代码中添加的:
mongoose.set('debug', true);
尝试连接像这样:
var db = mongoose.createConnection('mongodb://localhost:27017/mcfly');
并像这样访问你的collections:
var col = mongoose.model('mathpeep', new Schema({ firstname: String, lastname: String }),
'mathpeep' ) //here goes your collection name;