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

7
  • Please post your sample MongoDB documents. Commented Mar 9, 2016 at 15:33
  • Please post sample document here in your question. So that another person looking for similar issue, can take advantage of solution. Commented Mar 9, 2016 at 16:02
  • Also please let me know your MongoDB version? 3.2? Commented Mar 9, 2016 at 16:13
  • @Saleem mongo version is 2.6.11 & theres a link to the json in the question... its a large doc Commented Mar 9, 2016 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 Commented Mar 9, 2016 at 16:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this script is supported only on MongoDB version 3.2+
checkout new solution and should work on MongoDB 2.6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.