0

I have a collection named "categories". I want to be able to find one that has title "Juices". After targeting Juices I would like to be able to see all the items inside of it.

{ 
    "_id": "xibspjSNQLJKurHoT", 
    "title": "Juices", 
    "description": "all types of juices", 
    "items": [
        { 
            "title": "Apple Juice", 
            "description": "Made with apples", 
            "healthy": true 
        },
        { 
            "title": "Orange Juice", 
            "description": "Made with oranges", 
            "healthy": true 
        }
    ], 
    "author": "9MJh7d4ELJ9guChvs", 
    "createdAt": ISODate("2016-01-09T05:03:49.681Z") 
},
{ 
    "_id": "xibspjSNQLJKurHoT", 
    "title": "Meats", 
    "description": "beef", 
    "items": [
        { 
            "title": "Chicken", 
            "description": "Made with chicken", 
            "healthy": true 
        }
    ], 
"author": "9MJh7d4ELJ9guChvs", 
"createdAt": ISODate("2016-01-09T05:03:49.681Z") 

}

The result I would like to see is something like this

            { 
                "title": "Apple Juice", 
                "description": "Made with apples", 
                "healthy": true 
            },
            { 
                "title": "Orange Juice", 
                "description": "Made with oranges", 
                "healthy": true 
            }
Community
  • 1
  • 1
Jayswager
  • 211
  • 1
  • 2
  • 12
  • What do you mean by *I want to access the array named "items"*? can you show the expected result? – styvane Jan 09 '16 at 18:38
  • I added more details to it and the result i would like to get back. I'm new to using Embedded Data Models. – Jayswager Jan 09 '16 at 19:07
  • Possible duplicate of [How to select a single field in MongoDB?](http://stackoverflow.com/questions/25589113/how-to-select-a-single-field-in-mongodb) – styvane Jan 09 '16 at 19:20

1 Answers1

0

This is what gave me the results I wanted in case someone in the future looking for something similar to this.

    var Juices = db.Categories.findOne({title: "Juices"});
    var items = Juices.items // gives you the results of
[
    { 
        "title": "Apple Juice", 
        "description": "Made with apples", 
        "healthy": true 
    },
    { 
        "title": "Orange Juice", 
        "description": "Made with oranges", 
        "healthy": true 
    }
]

if someone knows a better way I'm happy to hear it (well.. see it)

Jayswager
  • 211
  • 1
  • 2
  • 12
  • The best way is to do this server-side. Check the link to the possible duplicate. `db.Categories.findOne( { 'title': 'Juices' }, { 'items': 1, '_id': 0 } )` – styvane Jan 09 '16 at 19:44