1

I have a document created on Mongoose DB with following structure:

{
"_id": {
    "$oid": "55bb2c92e4b0759bdca17fdc"
},
"email": "Customer1@xyz.com",
"serviceType": "Electricity",
"month": "JAN",
"year": "2015",
"bill": [
    {
        "PeakMsmt": "1",
        "OffPeakMsmt": "2",
        "Week": "Week1",
        "WeekEndTimeStamp": "2015-10-10T00:00:00.000Z"
    },
    {
        "PeakMsmt": "2",
        "OffPeakMsmt": "3",
        "Week": "Week2",
        "WeekEndTimeStamp": "2015-11-10T00:00:00.000Z"
    },
    {
        "PeakMsmt": "3",
        "OffPeakMsmt": "4",
        "Week": "Week3",
        "WeekEndTimeStamp": "2015-12-10T00:00:00.000Z"
    }
]
}

My Node.JS code is :

var mongoose   = require("mongoose");
mongoose.connect('mongodb://MY_MONGO_DB_URL');

var oub = require('./OptimizeUtilityBill');
oub.find({email:'Customer1@xyz.com',serviceType:'Electricity',month:'FEB',year:'2015'},function(err,oubDBResult){
        if(err){
            console.log("Error: "+err);
        }else{
            console.log("OUB result: "+oubDBResult);
        }
    });

my mongoose schema in node JS is:

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

var OptimizeUtilityBillSchema = new Schema({
    email:String,
    serviceType:String,
    month:String,
    year:String,
    bill:[{PeakMsmt:String,
        OffPeakMsmt:String,
        Week:String,
        WeekEndTimeStamp:String}]
});

module.exports = mongoose.model('optimizeUtilityBill', OptimizeUtilityBillSchema);

Please help, I am new to node.JS and mongoose both. I have spent hours trying to get it work and have also googled a lot about Mongoose schema. But was not able to query mongoose.

I get following result when I run this program :

enter image description here

user3769778
  • 967
  • 2
  • 7
  • 26

2 Answers2

0

Try this:

var mongoose   = require("mongoose");
var db = mongoose.connect('mongodb://MY_MONGO_DB_URL');
var oub = require('./OptimizeUtilityBill'); 

db.on('error', function(){console.log('connection error')});
db.once('open', function (callback) {
   oub.find({email:'Customer1@xyz.com',serviceType:'Electricity',month:'FEB',year:'2015'},function(err,oubDBResult){
    if(err){
        console.log("Error: "+err);
    }else{
        console.log("OUB result: "+oubDBResult);
    }
});
});

You're not waiting for the connection to actually be open.

Dave Briand
  • 1,684
  • 1
  • 16
  • 16
  • Running above code gives me error : stack trace `db.on('error', console.error.bind(console, 'connection error:')); ^ TypeError: undefined is not a function at Object. (C:\Users\CFG\Desktop\API\POC\TRXD\TestCode\app.js:20:4) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3` – user3769778 Aug 02 '15 at 16:28
  • Looks like an issue with the error callback, I made it simpler. – Dave Briand Aug 02 '15 at 19:23
0

The solution to my problem is here :

Mongoose find() not returning result

So to make it work, I need export my schema as :

module.exports = mongoose.model('optimizeUtilityBills', OptimizeUtilityBillSchema,'optimizeUtilityBills');

Note : optimizeUtilityBills is the name of my document in mongoDB.

thank you every one for your help.

Community
  • 1
  • 1
user3769778
  • 967
  • 2
  • 7
  • 26