1

This is simple but I'm slowly falling into madness, I got existing JSON imported to mongoDB:

{
    "_id": {
        "$oid": "5611acaca5ea5f7c5d1a1f41"
    },
    "abilities":[
        {
            "name":"name1"
        },
        {
            "name":"name2"
        }
    ]
}

in my app a character model:

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

var AbilitySchema = new Schema({
    name:String
});

var CharacterSchema = new Schema({
        abilities:[AbilitySchema]
});

var character = mongoose.model('character', CharacterSchema, 'character_collection');

module.exports = {
    Character: character
};

in my controller, my query:

Character.findOne({'abilities.name' : 'name1'}, function(err, data) {
    if (err) console.log(err);
    else console.log(data);
});

It seem through my search that this kind of query {'abilities.name' : 'name1'} would be the way to go for nested object but all my tries end up returning the whole dataset (which is a lot bigger than this abstraction) or null and I can't figure out how to query the attributes of my abilities and go down the hierarchy of that document.

Thanks

0

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.