Stack Exchange
Why $elemMatch vs. $and is giving different results?
I'm looking into Mongo University's sample sales database. Data conforms to below example:
{
  _id: ObjectId("5bd761dcae323e45a93ccfe8"),
  saleDate: ISODate("2015-03-23T21:06:49.506Z"),
  items: [
    {
      name: 'printer paper',
      tags: [ 'office', 'stationary' ],
      price: Decimal128("40.01"),
      quantity: 2
    },
    {
      name: 'notepad',
      tags: [ 'office', 'writing', 'school' ],
      price: Decimal128("35.29"),
      quantity: 2
    },
    {
      name: 'pens',
      tags: [ 'writing', 'office', 'school', 'stationary' ],
      price: Decimal128("56.12"),
      quantity: 5
    },
    {
      name: 'backpack',
      tags: [ 'school', 'travel', 'kids' ],
      price: Decimal128("77.71"),
      quantity: 2
    },
    {
      name: 'notepad',
      tags: [ 'office', 'writing', 'school' ],
      price: Decimal128("18.47"),
      quantity: 2
    },
    {
      name: 'envelopes',
      tags: [ 'stationary', 'office', 'general' ],
      price: Decimal128("19.95"),
      quantity: 8
    },
    {
      name: 'envelopes',
      tags: [ 'stationary', 'office', 'general' ],
      price: Decimal128("8.08"),
      quantity: 3
    },
    {
      name: 'binder',
      tags: [ 'school', 'general', 'organization' ],
      price: Decimal128("14.16"),
      quantity: 3
    }
  ],
  storeLocation: 'Denver',
  customer: { gender: 'M', age: 42, email: 'cauho@witwuta.sv', satisfaction: 4 },
  couponUsed: true,
  purchaseMethod: 'Online'
}

I had to write a query that finds the number of sales that included a laptop that cost less that $600. I used the following query:
db.sales.countDocuments({$and:[{"items.name":"laptop"}, {"items.price":{$lt:600}}]})

Which gives something like 2233 documents. Whereas the solution suggested by Mongo University is below which returns 397 documents:
db.sales.countDocuments({ items: { $elemMatch: { name: "laptop", price: { $lt: 600 } } } } )

I suspect that $and operator in this case is counting even the documents where "items.name":"laptop" and "items.price":{$lt:600} can be in different sub-documents. e.g. it will count a document that has following items in it:
items: [
    {
      name: 'laptop',
      tags: [ 'office', 'stationary' ],
      price: Decimal128("4000.00"),
      quantity: 2
    },
    {
      name: 'notepad',
      tags: [ 'office', 'writing', 'school' ],
      price: Decimal128("35.29"),
      quantity: 2
    }]

Since it has an item that is a laptop, and an item that has price < 600.
I'm trying to understand why my query gave incorrect result?

I'm a little confused about what the question is here since you've expressed the answer with your suspicion and that it's easy to test to confirm. This is also covered in the docs, perhaps most clearly for this example here

@user20042973 My knowledge of Mongodb is very limited. As you said, it is my suspicion, and I'm hoping to get corrected with an answer, if this is not the case. Thank you for the link you posted, I'll give it a read.

Looks like you have it perfectly right to me! I look forward to seeing the answer you post here after you do some further reading and experimenting
