Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I keep documents in aggregation with $unwind
    primarykey
    data
    text
    <p>Lets say I have three students...</p> <p>Alice, she is Always there on fridays.</p> <pre><code>{ "name" : "Alice", "goes" : { "mondays" : { "fr" : 900, "to" : 1400 }, "fridays" : { "fr" : 700, "to" : 1600 }, } } </code></pre> <p>And bob, here should be there on the first of january</p> <pre><code>{ "_id" : ObjectId("5284a7085d60338b40b8f17d"), "name" : "Bob", "goes" : { "mondays" : { "fr" : 800, "to" : 1200 }, "special" : [ { "date" : "2010-01-01", "fr" : 1000, "to" : 1500 } ] } } </code></pre> <p>And Clair who will not be attenging on mondays or at 10.00 </p> <pre><code>{ "_id" : ObjectId("5284c2785d60338b40b8f17f"), "name" : "Clair", "goes" : { "wednesdays" : { "fr" : 1100, "to" : 1500 }, "special" : [ { "date" : "2010-01-01", "fr" : 1600, "to" : 1900 }, { "date" : "2010-01-02", "fr" : 1000, "to" : 1300 } ] } } </code></pre> <p>I want to find all students that should attend on fridays at 7 och 10 on the first of January 2010</p> <p>So I do this with the aggregation framework.</p> <pre><code>db.students.aggregate( [ { $unwind: "$goes.special" }, { $match: { $or : [ { 'goes.fridays.fr': 700, }, { 'goes.special.date' : '2010-01-01', 'goes.special.fr': 1000 } ] } } ] ) </code></pre> <p>But Alice does not show up. It clearly states why in the mongodb docs, <a href="http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/" rel="nofollow">http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/</a> at the very bottom.</p> <blockquote> <p>"If you specify a target field for $unwind that holds an empty array ([]) in an input document, the pipeline ignores the input document, and will generates no result documents."</p> </blockquote> <p>I could solve it by adding an array with a null value in it but that does not seam like a nice solution.</p> <p>Is there a way I could get unwind NOT to ignore documents that does not have data in a $unwind'ed array?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload