0

I have created a simple Mongo database on mLab, and I want to print the content on my console using Node.js and Mongoose, but I'm not able to figure out what is wrong with my schema (or something else maybe?). It is not able to print nested object data.

Here is my database structure:

{
    "_id": {
        "$oid": "5a26d0a8f36d280fefe443ed"
    },
    "housename": "my sweet home",
    "rooms": [
        {
            "roomname": "kitchen",
            "appliance": "fridge"
        },
        {
            "roomname": "bedroom",
            "appliance": "lamp"
        }
    ]
}

I am trying to print this content like this:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userDataSchema = new Schema({housename: String, rooms: [{roomname: String, appliance: String}] }, {collection: 'testcollection'});
var userData = mongoose.model('userData', userDataSchema); 

var mongoDB = 'mongodb://homeuser:[email protected]:29776/homedb';
mongoose.connect(mongoDB, {
  useMongoClient: true
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

userData.find({}, function(err, data) {
    console.log(data);
});

The problem is, the console is not printing the nested rooms objects:

VSHANDIL-M-60PS:mongoclient vshandil$ node mongoclient.js 
[ { _id: 5a26d0a8f36d280fefe443ed,
    housename: 'my sweet home',
    rooms: [ [Object], [Object] ] } ]

Can anyone please help me figure out what might be going wrong? I am trying to follow the following doc:

http://mongoosejs.com/docs/guide.html

Thanks for any help!

1 Answer 1

4

It is just console.log behavior. Try console.log(JSON.stringify(data)); it will give you full object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.