0

We have a MongoDB collection with the following schema.

    Sales {
       salesID:"SO-0002"
       dispatches: [
          {
           "dispatchNo" : "SO-0002/dispatch/1",
        "date" : "2014-05-31T18:30:00.000Z",
        "location" : "l1",
        "items": [{
                     //itemDetails
                 }]
          },
          {
           "dispatchNo" : "SO-0002/dispatch/2",
        "date" : "2014-05-31T18:30:00.000Z",
        "location" : "l2",
        "items": [{
                     //itemDetails
                 }]
          }
       ]
    }

Upon execution of the following query:

db.sales.find({salesOrderNo:"SO-0002",'dispatches.dispatchNo':"SO-0002/dispatch/1"}).pretty()

We expect to get only the details of the dispatch 1, but we are still getting the details of all the dispatches.

Are we missing something?

Thanks.

Vivek Todi
  • 361
  • 1
  • 9
  • 24
  • Can you see if the answer to this question helps? http://stackoverflow.com/questions/24082174/get-elements-of-an-array-in-date-range – Tom Panning Jun 06 '14 at 15:17

1 Answers1

1

Yes. This is the expected behavior. dispatches is an array within the parent document. When a document is matched, the document is returned in its entirety. The only way around this is to change your schema into multiple collections, or to use the aggregation framework or map / reduce.

kwolfe
  • 1,663
  • 3
  • 17
  • 27