Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Query problem ($elemMatch misuse) #38

Closed
Volox opened this issue May 6, 2015 · 2 comments
Closed

Query problem ($elemMatch misuse) #38

Volox opened this issue May 6, 2015 · 2 comments

Comments

@Volox
Copy link

Volox commented May 6, 2015

Hello,
I have some problems with the query creation and i think the problems are related to the use of $elemMatch (here src/query.js).

My code is basically like this:

let query = {
  "date": {
      "$gte": "2015-05-05T00:00:00.000Z", // Not the string, the Date object
      "$lte": "2015-05-06T23:59:59.999Z" // Not the string, the Date object
    }
};

let result = yield* query.find( query );

But if i enable the DEBUG i see the generated query is:

monk:queries posts find {"date":{"$elemMatch":{"$gte":"2015-05-05T00:00:00.000Z","$lte":"2015-05-06T23:59:59.999Z"}}}

And i get no results while using the mongo console my query works.

The problem is related to the fact that $elemMatch works on array fields (see here)

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

@vadimdemedes
Copy link
Owner

Hello,

Yes, you are right, the problem is related to .where() implementation and how it treats objects. As a solution, I suggest the following:

let results = yield* Model.gte(startDate).lte(endDate).find();

Let me know how that works ;)

@vadimdemedes
Copy link
Owner

Pushed a change, which introduces a matches() method for $elemMatch usage and allow any object for where(), so any MongoDB operators can be used, in case they are not supported by Mongorito.

Thank you for posting this issue!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants