【问题标题】:Mongoose MongoDb not showing schemas or models in Node ShellMongoose MongoDb 未在 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 } }

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您需要在 shell 中 require 您的模型。在您调用 mongoose.model 之前,Mongoose 不会识别您已经定义了一个模型,并且只有在您需要 models/cpu.js 时才会调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2021-09-02
      • 2014-05-26
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多