Skip to content

Commit

Permalink
docs: update populate notes
Browse files Browse the repository at this point in the history
  • Loading branch information
leodinas-hao committed Apr 11, 2022
1 parent bf8b15c commit ae6eb34
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ parser.parse('filter={"$or":[{"key1":"value1"},{"key2":"value2"}]}&name=Telstra'
#### Populate operators

- Useful to populate sub-document(s) in query. Works with `MongooseJS`. Please see [Mongoose Populate](http://mongoosejs.com/docs/populate.html) for more details
- Supports deep populate with delimiter ":"
- Supports deep populate with delimiter ":" to indicate deep populate
- Below example & `test-populate.spec.ts` for more details
- See [Mongoose Docs](https://mongoosejs.com/docs/populate.html#deep-populate)
- Allows to populate with only selected fields
- Allows to populate with only selected fields with "." to indicate field selection
- Default operator key is `populate`

```js
Expand All @@ -117,7 +117,8 @@ parser.parse('populate=createdBy:friends.name,createdBy.name,likedBy.name');
// path: 'createdBy',
// select: 'name',
// populate: {
// path: 'name',
// path: 'friends',
// select: 'name',
// }
// }, {
// path: 'likedBy',
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-query-parser",
"version": "1.3.0",
"version": "1.3.1",
"description": "Convert url query string to MongooseJs friendly query object including advanced filtering, sorting, population, string template, type casting and many more...",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ class Tester {
@test('should parse deep populate')
deepPopulateParse() {
const parser = new MongooseQueryParser();
const qry = '_id=1&populate=p1,p2:p3.p4,p2:p3.p5,p6:p7';
const qry = '_id=1&populate=p1,p2:p3.p4,p2:p3:p5,p6:p7';
const parsed = parser.parse(qry);
assert.isNotEmpty(parsed.populate);
assert.isTrue(parsed.populate.length === 3);
for (const p of parsed.populate) {
if (p.path === 'p2') {
assert.isTrue(p.populate.path === 'p3');
assert.isTrue(p.populate.select.includes('p4'));
assert.isTrue(p.populate.select.includes('p5'));
assert.isTrue(p.populate.populate.path === 'p5');
}
if (p.path === 'p6') {
assert.isTrue(p.populate.path === 'p7');
Expand Down

0 comments on commit ae6eb34

Please sign in to comment.