0

Is there a reason I'm unaware of that would cause this query to fail?

Specifically it's the features: {$in: featuresArray} part that causes the query to fail.

Collection.find({
  features: {
    $in: featuresArray
  },
  location: {
    $near : {
      $geometry : {
        type : 'Point',
        coordinates: [long, lat]
      }
    }
  }},
 {
   sort: {
     score: -1
   }
});`
4
  • How did you define featureArray? Commented Oct 3, 2014 at 7:14
  • The sort spec is wrong here. But what is the error you are getting that makes you think the addition of searching on the features field is causing the problem? Could do with more information in your post. Commented Oct 3, 2014 at 7:20
  • This is using a JS framework, not in the shell, so yeah, the sort() is different. Commented Oct 3, 2014 at 15:26
  • 1
    This question appears to be off-topic because it is about a silly mistake I made in my code. It had to do with nesting, so this was more an oversight/typo error. Commented Oct 3, 2014 at 15:50

1 Answer 1

2

As the docs for db.collection.find says, the second argument for the find should be for projection - ie. which all fields to return in the result. But here you are using the second paramater to sort the results.

I think it should be done like :

Collection.find({
    features: {
        $in: featuresArray
    },
    location: {
        $near : {
            $geometry : {
                type : 'Point',
                coordinates: [long, lat]
            }
        }
    }
}).sort({
    score: -1
});
Sign up to request clarification or add additional context in comments.

2 Comments

Can you delete your answer? I realized my silly mistake and fixed my code. Had added a field and so thus needed to do 'features.name': featuresArray now.
This is using a JS framework, not in the shell, so yeah, the sort() is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.