Mongodb- using find() method on an Array of Objects only return first match instead of all

Question

Unlike the other question someone asked where they wanted only one item returned. I HAVE one item returned and I need ALL of the matching objects in the array return. However the second object that matches my query is being completely ignored.

This is what one of the items in the item collection looks like:

     {
       name: "soda",
       cost: .50,
       inventory: [
             { flavor: "Grape",
               amount: 8 },
             { flavor: "Orange",
               amount: 4 },
             { flavor: "Root Beer",
               amount: 15 }
       ]
     }

Here is the query I typed in to mongo shell:

    Items.find({"inventory.amount" : { $lte : 10} } , { name : 1,  "inventory.$.flavor" : 1})

And here is the result:

    "_id" : ObjectId("59dbe33094b70e0b5851724c"),
    "name": "soda"
    "inventory" : [
           { "flavor" : "Grape",
             "amount" : 8,
           }
     ]

And here is what I want it to return to me:

    "_id" : ObjectId("59dbe33094b70e0b5851724c"),
    "name": "soda"
    "inventory" : [
           { "flavor" : "Grape",
             "amount" : 8
           },
           { "flavor" : "Orange",
             "amount" : 4
           }
     ]

I'm new to mongo and am dabbling to get familiar with it. I've read through the docs but couldn't find a solution to this though it's quite possible I overlooked it. I'd really love some help. Thanks in advance.


Show source
| find   | mongodb   | mongo-shell   2017-10-12 00:10 0 Answers

Answers to Mongodb- using find() method on an Array of Objects only return first match instead of all ( 0 )

Leave a reply to - Mongodb- using find() method on an Array of Objects only return first match instead of all

◀ Go back