Mongoose MongoDb 未在 Node Shell 中显示模式或模型
Posted
技术标签:
【中文标题】Mongoose MongoDb 未在 Node Shell 中显示模式或模型【英文标题】:Mongoose MongoDb not showing schemas or models in Node Shell 【发布时间】:2014-07-21 23:49:16 【问题描述】:我是 mongoose 和 mongodb 的新手。创建我的模式和模型后:
one@demo ~/cloudimageshare-monitoring/project/app/data $ tree
.
|-- models
| |-- cpu.js
| |-- httpPort.js
| |-- memory.js
| `-- network.js
`-- schemas
|-- cpu.js
|-- http_port.js
|-- memory.js
`-- network.js
2 directories, 8 files
one@demo ~/cloudimageshare-monitoring/project/app/data $ cat models/cpu.js:
var mongoose = require('mongoose');
var CpuSchema = require(../schemas/cpu);
var Cpu = mongoose.model('Cpu', CpuSchema);
module.exports = Cpu;
one@demo ~/cloudimageshare-monitoring/project/app/data $ cat schemas/cpu.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
var CpuSchema = new Schema(
'timeStamp' : type : Date, index: true ,
"avaiable" : Boolean,
"status" : String,
"metrics" : [ "15m": [ "data" : Number,
"type" : String,
"unit" : String
],
"5m": [ "data" : Number,
"type" : String,
"unit" : String
],
"1m": [ "data" : Number,
"type" : String,
"unit" : String
]
]
)
module.exports = CpuSchema;
one@demo ~/cloudimageshare-monitoring/project/app/data $
在节点外壳中连接到数据库时,我没有显示任何模式或模型。这是为什么呢?
> var dbURL = 'mongodb://localhost/database';
undefined
> var db = require('mongoose').connect(dbURL);
undefined
> db
> db
connections:
[ base: [Circular],
collections: ,
models: ,
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'database',
options: [Object],
otherDbs: [],
_readyState: 1,
_closeCalled: false,
_hasOpened: true,
_listening: true,
_events: ,
db: [Object] ],
plugins: [],
models: ,
modelSchemas: ,
options: pluralization: true
【问题讨论】:
【参考方案1】:您需要在 shell 中 require
您的模型。在您调用 mongoose.model
之前,Mongoose 不会识别您已经定义了一个模型,并且只有在您需要 models/cpu.js
时才会调用它。
【讨论】:
以上是关于Mongoose MongoDb 未在 Node Shell 中显示模式或模型的主要内容,如果未能解决你的问题,请参考以下文章
详解node+mongoose+mongoDb(mongoDb安装运行,在node中链接)
??? MongoDB ??? Node.js ????????? Mongoose ??