mongodb - Query to filter array value property -
suppose have following data in mongo:
{ "id": "foo1", "description": "myfoo1", "bars": [ { "id": "foo1bar1", "description": "myfoo1bar1", "builton": "2010-03-01t05:00:00.000z" }, { "id": "foo1bar2", "description": "myfoo1bar2", "builton": "2011-03-01t05:00:00.000z" } ] } { "id": "foo2", "description": "myfoo2", "bars": [ { "id": "foo2bar1", "description": "myfoo2bar1", "builton": "2012-03-01t05:00:00.000z" }, { "id": "foo2bar2", "description": "myfoo2bar2", "builton": "2013-03-01t05:00:00.000z" } ] }
my question two-fold, suppose:
is possible execute query return documents have bar
date between specified range , return bars
fall in date range? example, if date range 2010-02-01 through 2010-04-01 i'd want back:
{ "id": "foo1", "description": "myfoo1", "bars": [ { "id": "foo1bar1", "description": "myfoo1bar1", "builton": "2010-03-01t05:00:00.000z" } ] }
is best way structure data or should more relationally (i.e. have 2 separate documents (foos
, bars
) , have fooid
field on bar
)?
in general, full document back. if want 1 element of sub array, best bet split each "bar" it's own document. in case, better use 2 collections you'd in rdbms also possible store things this:
{ "foo" : { "id": "foo1", "description": "myfoo1", } "id": "foo1bar1", "description": "myfoo1bar1", "builton": "2010-03-01t05:00:00.000z" }, { "foo" : { "id": "foo1", "description": "myfoo1", } "id": "foo1bar2", "description": "myfoo1bar2", "builton": "2011-03-01t05:00:00.000z" }
ie, store "foo" information each bar.
Comments
Post a Comment