nevi_me
nevi_me

Reputation: 2730

Mongoosejs: combining two 'query.where' clauses with 'or'

I have a mongoosejs Query, which I am trying to execute.

I have:

query.where('loc').nearSphere(loc).maxDistance(range/6378.137);
query.where('name', name );

I want to combine the two queries without using this.find( $or: {} ).

The code is supposed to either find a document where the location is within a range, or the name matches the name provided.

Is it possible to create the query using query.or()?

Upvotes: 1

Views: 879

Answers (1)

nevi_me
nevi_me

Reputation: 2730

I figured out the answer:

query.or([
    {
        nearSphere: 'loc',
        near: loc,
        maxDistance: (range/6378.137) },
    {
        "name": name
    }
]);

The part I couldn't understand was how to embed a query inside the $or clause.

I thought of deleting the question, but I left it here with this solution in case it's ever helpful to someone

Upvotes: 2

Related Questions