Mongoose说我有46条记录,mongoDB说我有零?

时间:2011-05-17 05:46:23

标签: mongodb node.js mongoose

以下测试脚本说我有46条记录:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var DealSchema = new Schema({
  title : String,
});

var Deal = mongoose.model('Deal', DealSchema);  

mongoose.connect('mongodb://localhost/dealsite');
mongoose.connection.on("open", function(){
  console.log("Mongoose connected");
  Deal.count({}, function( err, count){
    console.log( "Records:", count );
  })
});

输出:

$ node testmongo.js 
Mongoose connected
Records: 46

如果我尝试使用mongo shell读取线索,我会得到一个不同的故事:

$ mongo localhost/dealsite
MongoDB shell version: 1.4.4
url: localhost/dealsite
connecting to: localhost/dealsite
type "exit" to exit
type "help" for help
> db.dealsite.count()
0
> db.dealsite.Deal.count()
0

我的记录隐藏在哪里?

2 个答案:

答案 0 :(得分:5)

在MongoDB中,尝试:

use dealsite
db.deals.count()

如果您创建名为“Deal”的Mongoose模型,它将创建并使用名为“deals”的MongoDB集合。

答案 1 :(得分:2)

你错过了声明:

"use Deal"    [ use DATABASE_NAME ]

之前

db.dealsite.count()