Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
mongoose-string-query
Advanced tools
Updated Fork from original, Given a Mongoose model and an array of URI params, construct a query for use in a search API.
This is a working and updated fork from the great mongoose-api-query package that is deprecated and hasn't been updated for 3 years.
If you use Mongoose to help serve results for API calls, you might be used to handling calls like:
/monsters?color=purple&eats_humans=true
mongoose-query handles some of that busywork for you. Pass in a vanilla object (e.g. req.query) and query conditions will be cast to their appropriate types according to your Mongoose schema. For example, if you have a boolean defined in your schema, we'll convert the eats_humans=true
to a boolean for searching.
It also adds a ton of additional search operators, like less than
, greater than
, not equal
, near
(for geosearch), in
, and all
. You can find a full list below.
When searching strings, by default it does a partial, case-insensitive match. (Which is not the default in MongoDB.)
Apply the plugin to any schema in the usual Mongoose fashion:
monsterSchema.plugin(mongooseStringQuery);
Then call it like you would using Model.find
. This returns a Mongoose.Query:
Monster.apiQuery(req.query).exec(...
Or pass a callback in and it will run .exec
for you:
Monster.apiQuery(req.query, function(err, monsters){...
const mongooseStringQuery = require('mongoose-string-query')
t
, y
, and 1
are all aliases for true
:
/monsters?eats_humans=y&scary=1
Match on a nested property:
/monsters?foods.name=kale
Use exact matching:
/monsters?foods.name={exact}KALE
Matches either kale
or beets
:
/monsters?foods.name=kale,beets
Matches only where kale
and beets
are both present:
/monsters?foods.name={all}kale,beets
Numeric operators:
/monsters?monster_id={gte}30&age={lt}50
Combine operators:
/monsters?monster_id={gte}30{lt}50
geo near, with (optional) radius in miles:
/monsters?latlon={near}38.8977,-77.0366
/monsters?latlon={near}38.8977,-77.0366,10
/monsters?page=2
/monsters?page=4&per_page=25 // per_page defaults to 10
/monsters?sort_by=name
/monsters?sort_by=name,desc
Do you have a property defined in your schema like data: {}
, that can have anything inside it? You can search that, too, and it will be treated as a string.
This is a list of the optional search operators you can use for each SchemaType.
number={all}123,456
- Both 123 and 456 must be presentnumber={nin}123,456
- Neither 123 nor 456number={in}123,456
- Either 123 or 456number={gt}123
- > 123number={gte}123
- >= 123number={lt}123
- < 123number={lte}123
- <=123number={ne}123
- Not 123number={mod}10,2
- Where (number / 10) has remainder 2string={all}match,batch
- Both match and batch must be presentstring={nin}match,batch
- Neither match nor batchstring={in}match,batch
- Either match or batchstring={not}coffee
- Not coffeestring={exact}CoFeEe
- Case-sensitive exact match of "CoFeEe"latlon={near}37,-122,5
Near 37,-122, with a 5 mile max radiuslatlon={near}37,-122
Near 37,-122, no radius limit. Automatically sorts by distancenode load_fixtures.js
node app.js
mocha
FAQs
Updated Fork from original, Given a Mongoose model and an array of URI params, construct a query for use in a search API.
We found that mongoose-string-query demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.