Skip to content

Commit 56a464c

Browse files
authored
Major refactoring, including support for mongoose 4.x (#13)
major refactoring including several fixes and more tests * Update package.json * fixes against latest mongoose. * This drops `Model.Query` api because it seems to be internally used by mongoose. Use `Model.query` instead. * split module to smaller parts * add parseQuery tests * add more promise supports (flat) * several fixes popups during parseQuery tests, e.g. date parsing * moving to ES6 age.. - require node.js 6.x . * update documentation
1 parent 070f7b0 commit 56a464c

File tree

7 files changed

+620
-484
lines changed

7 files changed

+620
-484
lines changed

README.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ mongoose-query [![Build Status](https://travis-ci.org/jupe/mongoose-query.png?br
55

66
[![NPM](https://nodei.co/npm-dl/mongoose-query.png)](https://nodei.co/npm/mongoose-query/)
77

8-
mongoose query creator. Alternative for mongoose-api-query but without schema understanding.
9-
This very simple library can be used for example expressjs+mongoose applications to help
10-
construct mongoose query model directly from url parameters.
8+
This library is usefull example with `expressjs` + `mongoose` applications to help construct mongoose query model directly from url parameters. Example:
119

12-
## History
10+
```
11+
http://localhost/model?q={"group":"users"}&f=name&sk=1&l=5&p=name
12+
```
13+
Converted to:
14+
```
15+
model.find({group: "users"}).select("name").skip(1).limit(5).populate('name')
16+
```
17+
18+
## Changes log
1319

1420
|versio|Changes|
1521
|------|-------|
22+
|0.3.0|Big refactoring, see more from release note.. e.g. mongoose 4.x support|
1623
|0.2.1|added oid support, fixed aggregate and support mongoose => 3.8.1
1724
|0.2.0|replace underscore with lodash, possible to return promise when no callback in use|
1825
|0.1.7|typo on mapReduce case, !empty keyword added|
@@ -42,32 +49,37 @@ module.exports = function query(req, res) {
4249
}
4350
```
4451

45-
## Query string example
52+
## doc
4653

4754
```
48-
http://localhost/query.json?q={"group":"users"}&f=name&sk=1&l=5&p=name
49-
50-
Converted to:
51-
52-
model.find({group: "users"}).select("name").skip(1).limit(5).populate('name')
55+
var QueryPlugin = require(mongoose-query);
56+
schema.plugin( QueryPlugin(, <options>) )
5357
```
58+
optional `options`:
59+
* `logger`: custom logger, e.g. winston logger, default: "dummy logger"
60+
* `allowEval`: <boolean> Allow to use eval or not, default: true
5461

55-
**note:** Seems that sorting with limit doesn't work with mongodb 2.4.x without related indexes. Mongodb 2.6.x seems to work.
62+
Model static methods:
5663

64+
`model.query( <query>(, <callback>) )`
5765

58-
## doc
66+
`model.leanQuery(<query>(, <callback>) )`: gives plain objects ([lean](http://mongoosejs.com/docs/api.html#query_Query-lean))
67+
68+
**Note:** without `<callback>` you get Promise.
5969

70+
**URL API:**
6071
```
61-
http://www.myserver.com/query?[q=<query>][&t=<type>][&f=<fields>][&s=<order>][&sk=<skip>][&l=<limit>][&p=<populate>][&fl=<boolean>][&map=<mapFunction>][&reduce=<reduceFunction>]
72+
http://www.myserver.com/query?[q=<query>][&t=<type>][&f=<fields>][&s=<order>][&sk=<skip>]
73+
[&l=<limit>][&p=<populate>][&fl=<boolean>][&map=<mapFunction>][&reduce=<reduceFunction>]
6274
6375
q=<query> restrict results by the specified JSON query
6476
regex e.g. q='{"field":{"$regex":"/mygrep/", "$options":"i"}}'
6577
t=<type> find|findOne|count|aggregate|distinct|aggregate|mapReduce
66-
f=<set of fields> specify the set of fields to include or exclude in each document
78+
f=<set of fields> specify the set of fields to include or exclude in each document
6779
(1 - include; 0 - exclude)
68-
s=<sort order> specify the order in which to sort each specified field
80+
s=<sort order> specify the order in which to sort each specified field
6981
(1- ascending; -1 - descending), JSON
70-
sk=<num results to skip> specify the number of results to skip in the result set;
82+
sk=<num results to skip> specify the number of results to skip in the result set;
7183
useful for paging
7284
l=<limit> specify the limit for the number of results (default is 1000)
7385
p=<populate> specify the fields for populate, also more complex json object is supported.
@@ -83,23 +95,23 @@ Special values:
8395
"oid:<string>" string is converted to ObjectId
8496
{ $regex: /<string>/, regex match with optional regex options
8597
($options: "") }
86-
98+
8799
Alternative search conditions:
88100
"key={in}a,b" At least one of these is in array
89101
"key={nin}a,b" Any of these values is not in array
90102
"key={all}a,b" All of these contains in array
91-
"key={empty}-" Field is empty or not exists
92-
"key={!empty}-" Field exists and is not empty
103+
"key={empty}" Field is empty or not exists
104+
"key={!empty}" Field exists and is not empty
93105
"key={mod}a,b" Docs where key mod a is b
94106
"key={gt}a" Docs key is greater than a
95107
"key={lt}a" Docs key is lower than a
96108
"key=a|b|c" Docs where type of key is Array and contains at least one of given value
109+
```
97110

98-
Results:
99-
100-
fl=false
111+
Results with `fl=false`:
112+
```
101113
[
102-
{
114+
{
103115
nest: {
104116
ed: {
105117
data: 'value',
@@ -108,14 +120,12 @@ fl=false
108120
}
109121
}
110122
]
123+
```
111124

112-
fl=true
125+
Results with `fl=true`:
126+
```
113127
[
114128
{'nest.ed.data': 'value',
115129
'nest.ed.data2':'value'},
116130
]
117131
```
118-
119-
120-
121-

0 commit comments

Comments
 (0)