Vijay Kasina
Vijay Kasina

Reputation: 144

How to get only part of the document while querying with mongoose?

I have following

{
    "_id" : ObjectId("56d808d826941f84bc697b4e"),
    "outerArray" : [ 
        {
            "middleArray" : [ 
                {
                    "someKey": "value",
                    "innerArray" : [ 
                        "F999-99999", 
                        "F999-999999", 
                        "FAF-99999", 
                        "FSF-99999", 
                        "FSW-99999", 
                        "FSX-99999", 
                        "FAF-999999", 
                        "FSF-999999", 
                        "FSW-999999", 
                        "FSX-999999"
                    ]
                }
            ]
        }
    ]
}

when I query

Model.find({outerArray.middleArray.someKey = 'value'},outerArray.middleArray.innerArray)},function(err,results){
//do some thing
}

I am getting innerArray along with the outer structure. But what i want is only innerArray part like -

"innerArray" : [ 
                        "F999-99999", 
                        "F999-999999", 
                        "FAF-99999", 
                        "FSF-99999", 
                        "FSW-99999", 
                        "FSX-99999", 
                        "FAF-999999", 
                        "FSF-999999", 
                        "FSW-999999", 
                        "FSX-999999"
                    ]

so i dont need to traverse through all outer arrays like outerArray[0].middleArray[0].innerArray

I looked into projections but i am not able to find anything. Please help me with this. Thanks a Lot ...

Upvotes: 0

Views: 247

Answers (2)

Derick Bailey
Derick Bailey

Reputation: 72878

You can't do what you want, directly.

MongoDB is a document database, not a relational database. When you retrieve (find) a document, it will retrieve the entire document, always.

If you only want the inner array, you need to pull that out of the full document that was returned to you.

Upvotes: 1

num8er
num8er

Reputation: 19372

how about this?

var _ = require('lodash');

var data = {
    "_id" : "SOMETHING",
    "outerArray" : [
        {
            "middleArray" : [
                {
                    "someKey": "value",
                    "innerArray" : [
                        "F999-99999",
                        "F999-999999",
                        "FAF-99999",
                        "FSF-99999",
                        "FSW-99999",
                        "FSX-99999",
                        "FAF-999999",
                        "FSF-999999",
                        "FSW-999999",
                        "FSX-999999"
                    ]
                }
            ]
        }
    ]
}

data = _.result(data, "outerArray[0].middleArray[0].innerArray", null);
console.log(data);

Also have You tried this ?

Model.find(
    {outerArray.middleArray.someKey: 'value'},
    'outerArray.middleArray.innerArray',
    function(err,results){
        console.log(results);
    });

Upvotes: 1

Related Questions