Utility function that maps a JSON object to mongoose operators, works with node 10.
This is useful for forwarding graphql request params to either the mongoose model or the mongodb collection. Similar to how Prisma and Strapi work.
You pass the collection and the search parameters and get the back the expected result using for example
const result = await useSearchParams(Test, {
where: {
title_contains: "lore",
}
});
res.send(result);
npm i mongo-search-parameters
OR
yarn add mongo-search-parameters
- Mongoose: https://glitch.com/~mongo-search-parameters-plain
- Apollo Graphql + Mongoose: https://glitch.com/~mongo-search-parameters-with-apollo-server
import useSearchParams from 'mongo-search-parameters';
import { mongooseModels } from 'path-to-your-mongoose-models';
const { User } = mongoModels;
// where (object): Define the operators to apply in the query.
const params = { // all fields are optional
where: {
name_contains: 'Test',
age_gt: 21
},
sort: 'name:desc', // it does accept also an array like ['name:desc', 'age:asc']
limit: 10
}
// the above query will find all users which names contain (case insensitive) 'Test' and age is greater than 21, sort them by name from Z to A, limit them to just 10 rows if more are returned
export default async () => {
const categories = await useSearchParams(User, { ...params });
return categories
}
All mongo and mongoose chain methods and options are still allowed. For example for:
- mongoose you can do
await useSearchParams(User, { ...params }, { age: 1 }).lean()
- mongo you can do
await useSearchParams(User, { ...params }, { projection: { age: 1 } }).toArray()
- where | ne
- where | gt
- where | gte
- where | lt
- where | lte
- where | in
- where | nin
- where | contains
- where | containss
- where | matches
- where | matchess
- where | containsIndex
- where | containssIndex
- where | matchesIndex
- where | matchessIndex
- limit
- start
- sort
name_contains: 'Mark Twain'
: the field name in the db is a string and contains the substring 'Mark Twain'. Mark twain, mark twain, mark Twain, etc. would be returned. It is case insentitive
name_containsIndex: 'Mark Twain'
: like name_contains
but uses indexes. Do not forget to set a text index on the field name
and that the MongoDB version you use supports text indexes as well
name_contains: 'Mark'
: the field name in the db is a string and contains the substring 'Mark Twain'. Mark Twain, would be returned while mark Twain will not. It is case sensitive
name_containssIndex: 'Mark Twain'
: like name_containss
but uses indexes. Do not forget to set a text index on the field name
and that the MongoDB version you use supports text indexes as well
name_matches: 'Mark Sculby'
: the field name in the db is a string and matches the substring 'Mark' OR the substring 'Sculby'. Mark Johnson, Vincent Skulby, etc. would be returned. It is case insentitive
name_matchesIndex: 'Mark Twain'
: like name_matches
but uses indexes. Do not forget to set a text index on the field name
and that the MongoDB version you use supports text indexes as well
name_matchess: 'Mark Sculby'
: the field name in the db is a string and matches the substring 'Mark' OR the substring 'Sculby'. Mark Johnson, Vincent Skulby, etc. would be returned while mark Johnson would not. It is case sentitive
name_matchessIndex: 'Mark Twain'
: like name_matchess
but uses indexes. Do not forget to set a text index on the field name
and that the MongoDB version you use supports text indexes as well
age_ne: 21
: the field age does not equal to 21. Can be used with any field type.
age_gt: 21
: greater than
age_gte: 21
: greater than or equal to
age_lt: 21
: less than
age_lte: 21
: greater than or equal to
age_in: [21, 26, 27, 28, 21]
: true if field is contained in the array of values supplied. Accepts also single value like age_in: 21
age_nin: [21]
: true if field is NOT contained in the array of values supplied
- Add mongodb client compatibility
- Add demo
- Refine search and explain different behaviors
- Allow to search using text indexes
- Implement OR operator