首页 文章

将mongoose与预先存在的mongo集合连接起来

提问于
浏览
0

我现在已经看了一会儿而且我被卡住了 . 我也提到了this问题和this问题并尝试了,但我还没能让它运行起来 .

这是我的项目的简要概述:我有一个python脚本,每天解析服务器上某些日志的数据 . 每个日志都是一个python对象,它存储在一个集合中,相应的日期位于mongoDB的db'staticlogs'中 .

我现在已经安装了node和mongoose,我试图从名为-130702的特定集合中提取这些数据 . 我收到以下错误:

C:\ node_app \ node_modules \ mongodb \ lib \ mongodb \ connection \ server.js:570 throw err; ^ ReferenceError:未定义架构

我的stabilitylog.js看起来像这样:

var mongoose = require('mongoose');
            mongoose.connect('mongodb://localhost/stabilitylogs');

            var db = mongoose.connection;
            db.on('error', console.error.bind(console, 'connection error:'));
            db.once('open', function callback () {
                var rackSchema=new Schema({
                    _id: {type: String },
                    Headend:{
                        Name: String,
                        Rack: {
                            TestInfo:{
                                ScriptStart: String,
                                TotalReboots: String,
                                ScriptLoopCount: String,
                                ScriptName: String,
                                ScriptStop: String,
                                SystemName: String,
                                TotalSTBs: String,
                                PerReboots: String,
                                RunTime: String,
                                BuildVer: String
                                },
                            StbData: [{
                                Status: String,
                                UpTime: String,
                                DBType: String,
                                IP: String,
                                DBVersion: String,
                                RebootData: String,
                                MAC: String,
                                MWApp: String,
                                OS: String
                                },],
                            Number: String
                            }
                        }
                    },
                {collection: '130702'});

                var doc = mongoose.model(rackschema, '130702');
                doc.find();
                });

我对此非常陌生,并确保我的代码中有很多错误,但我真的需要一些帮助 . 我在Windows 7上运行我的整个应用程序 - 使用MSI安装程序和nodeJS - v0.10.12安装mongodb ver2.2.4 . 我通过编写package.json文件在npm install中安装了mongodb和mongoose模块 .

任何帮助都非常感谢 . 另外,如果需要更多信息,请告诉我 .

1 回答

  • 1

    除非你的文件中已经有 var Schema = mongoose.Schema; ,否则你需要像JohnnyHK那样使用 new mongoose.Schema . 另外,如果你想让查询立即执行,那么mongoose模型上的find method应该看起来像这样 .

    doc.find({}, function(err,collection){ 
    //do something with the collection
    });
    

相关问题