0

Here's a snapshot of my document: enter image description here

I would like to know how can i retrieve only the documents where ANY of the breakout items has a property named source.

I tried the following: db.getCollection('receipts').find({"sizeBreakout.packBreackout.breakout.source":{"$exists":true}})
but a empty result is being returned always... why?!? what is the correct syntax for this query?!? Edit1:
Attached File: https://drive.google.com/file/d/0B2zKseaQl2gnVlFWZFlaRzloMDg/view?usp=sharing

Leonardo
  • 8,477
  • 8
  • 47
  • 119
  • Please post your sample MongoDB documents. – Saleem Mar 09 '16 at 15:33
  • Please post sample document here in your question. So that another person looking for similar issue, can take advantage of solution. – Saleem Mar 09 '16 at 16:02
  • Also please let me know your MongoDB version? 3.2? – Saleem Mar 09 '16 at 16:13
  • @Saleem mongo version is 2.6.11 & theres a link to the json in the question... its a large doc – Leonardo Mar 09 '16 at 16:54
  • Well, is it possible for you to upgrade mongo? I can write query for 2.6 but it will not be as efficient as for 3.2 – Saleem Mar 09 '16 at 16:57
  • @Saleem not my choice mate... i got stuck with it... – Leonardo Mar 09 '16 at 17:14
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – Blakes Seven Mar 09 '16 at 23:31

1 Answers1

1

Well, if you have MongoDB 3.2, you can use this simple query.

MongoDB version 3.2+

db.docs.find({$filter: {input: "$sizeBreakout.packBreackout.breakout", 
  as: breakout,
  cond:{$exists:{"$$breakout.source": true}}}})

MongoDB version < 3.2

db.docs.aggregate([
{$unwind: "$sizeBreakout.packBreakout.breakout"},
{$match: {"sizeBreakout.packBreakout.breakout.source":{$exists: true}}}
])

This query will find all documents where sizeBreakout.packBreackout.breakout.source exists.

Saleem
  • 7,965
  • 2
  • 16
  • 31