Skip to content

Instantly share code, notes, and snippets.

@keqiang
Last active April 19, 2018 15:59
Show Gist options
  • Save keqiang/2195c7468474c0f3339b7f38d5e1b442 to your computer and use it in GitHub Desktop.
Save keqiang/2195c7468474c0f3339b7f38d5e1b442 to your computer and use it in GitHub Desktop.

I have a mongodb instance running at localhost:27017

Then I imported the below grades.json into mongo database 'backend' by mongoimport --db backend --collection grades --drop --file ./grades.json

Next I created a feathers application called 'backend' by feathers generate app

Then created a service called 'grades', configured as 'mongoDB' and set the db link to 'localhost:27017/backend' by feathers generate service

Start the app by npm start at localhost:3030

Used postman to request localhost:3030/grades and got all the records

However when I tried to use localhost:3030/grades?grade[$lt]=90, it returned 0 records. Even when requesting localhost:3030/grades?grade=90 it fails.

My attempt: I tried adding a 'before' hook to service 'grades' as:

      function(context) {
        const query = context.params.query;

        if (query.grade !== undefined) {
          context.params.query.grade = parseInt(query.grade);
        }
      }

This makes localhost:3030/grades?grade=90 work, however the filtering by $lt still doesn't work.

{ "student" : "Joe", "assignment" : "hw1", "grade" : 90 }
{ "student" : "Joe", "assignment" : "hw2", "grade" : 80 }
{ "student" : "Joe", "assignment" : "hw3", "grade" : 85 }
{ "student" : "Joe", "assignment" : "exam", "grade" : 100 }
{ "student" : "Steve", "assignment" : "hw1", "grade" : 80 }
{ "student" : "Steve", "assignment" : "hw2", "grade" : 90 }
{ "student" : "Steve", "assignment" : "hw3", "grade" : 100 }
{ "student" : "Steve", "assignment" : "exam", "grade" : 100 }
{ "student" : "Amanda", "assignment" : "hw1", "grade" : 100 }
{ "student" : "Amanda", "assignment" : "hw2", "grade" : 90 }
{ "student" : "Amanda", "assignment" : "hw3", "grade" : 80 }
{ "student" : "Amanda", "assignment" : "exam", "grade" : 100 }
{ "student" : "Susan", "assignment" : "hw1", "grade" : 100 }
{ "student" : "Susan", "assignment" : "hw2", "grade" : 90 }
{ "student" : "Susan", "assignment" : "hw3", "grade" : 85 }
{ "student" : "Susan", "assignment" : "exam", "grade" : 80 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment