Loop through Mongoose objects Loop through Mongoose objects mongoose mongoose

Loop through Mongoose objects


Well, question is not fresh, but this situation may still become a piece of pain. You may use toJSON or toObject method to get normal, iterable object. Just like this:

media = media.toJSON()Object.keys(media.audio).forEach(...)

See this post for details about toJSON an toObject


I think you can do it with:

var media = {    _id: '558d0b3a5fa02e7e218b470c',    name: '54ae5d587860b4.61272171',    cover: true,    size: 144,    audio: {        es: '54ae5ec1e31c68.83020900',        en: '54ae5ec240dd98.65329366',        fr: '54ae5ec2950737.15028179',        pt: '54ae5ec2e3bcf5.08479198',        ar: '54ae5ec33ab8f5.67860635',        ru: '54ae5ec393d300.66214842'    },    comments: []};for (var property in media.audio) {    if (media.audio.hasOwnProperty(property)) {        console.log(property);    }}