mongo-query-builder-chain
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -22,2 +22,6 @@ const mongoQBModule = require('./module'); | ||
.addToQuery(['field3'], 0) | ||
.addToQuery([], mongoQB.$language()) | ||
.projectionAdd(['projection_field2', 'projection_field3', 'projection_field4']) | ||
.projectionAdd(['projection_field1', 'projection_field5'], 0) | ||
.projectionRemove('projection_field2') | ||
.sort("field0", 1) | ||
@@ -27,4 +31,3 @@ .sort("field1.field11.field111", 1) | ||
.sort("field4", -1) | ||
.$searchRemove() | ||
.getValue() | ||
); |
@@ -260,2 +260,27 @@ var helpers = require('./helpers'); | ||
$language(value = 'none') { | ||
if (helpers.isOfPrimitiveType(['string'], value)) { | ||
if (!this.body.query.$text) { | ||
this.body.query['$text'] = {}; | ||
} | ||
this.body.query.$text['$language'] = value; | ||
return this.body.query.$text; | ||
} else { | ||
throw "Argument value must be string!"; | ||
} | ||
} | ||
$languageRemove() { | ||
if (this.body.query.$text) { | ||
delete this.body.query.$text.$language; | ||
if (!Object.keys(this.body.query.$text).length) { | ||
delete this.body.query.$text; | ||
} | ||
} | ||
return this; | ||
} | ||
$search(value) { | ||
@@ -296,4 +321,44 @@ if (helpers.isOfPrimitiveType(['string'], value)) { | ||
} | ||
// Projection | ||
projectionAdd(keys, include = 1) { | ||
if (include === 0 || include === 1) { | ||
if (Array.isArray(keys)) { | ||
keys.forEach(key => { | ||
if (helpers.isOfPrimitiveType(['string'], key)) { | ||
if (!this.body.$projection) { | ||
this.body['$projection'] = {}; | ||
} | ||
this.body.$projection[key] = include; | ||
} else { | ||
throw "Argument keys contains a non string element!"; | ||
} | ||
}); | ||
return this; | ||
} else { | ||
throw "Argument keyPath must be an array!"; | ||
} | ||
} else { | ||
throw "Argument include must be 0 or 1!"; | ||
} | ||
} | ||
projectionRemove(key) { | ||
let projectionKeys = Object.keys(this.body.$projection); | ||
if (projectionKeys.includes(key)) { | ||
delete this.body.$projection[key]; | ||
if (!projectionKeys.length) { | ||
delete this.body.$projection; | ||
} | ||
return this; | ||
} else { | ||
throw "The key does not exist!"; | ||
} | ||
} | ||
} | ||
exports.MongoQuery = MongoQuery; |
@@ -20,3 +20,3 @@ { | ||
"type": "module", | ||
"version": "1.0.4" | ||
"version": "1.0.5" | ||
} |
@@ -93,2 +93,14 @@ # mongo-query-builder-chain | ||
Adds a text language by a specified _value_ (the default _value_ is "none") | ||
```javascript | ||
mongoQB.$language(value) | ||
``` | ||
It returns the object containing only the **$text** operator and its value with the **$language** operator as key and its value provided by the argument. If the **$text** key does not exist in the query, it is added automatically. | ||
Removes the current text language | ||
```javascript | ||
mongoQB.$languageRemove() | ||
``` | ||
If the **$language** key is the last remaining in the **$text** operator, the **$text** key will be removed automatically. It returns the current state of the _mongoQuery_ object. | ||
#### Date operators | ||
@@ -118,2 +130,22 @@ | ||
## Projecting | ||
Add a projection criteria with keys _field1_, _field2_, _field3_ intended to be excluded | ||
```javascript | ||
mongoQuery.projectionAdd(['field1', 'field2', 'field3'], 0); | ||
``` | ||
The second argument, denoting the inclusion of the fields, accepts only 0 and 1 as values. It returns the current state of the mongoQuery object. | ||
Add a projection criteria with keys _field4_, _field5_ intended to be included | ||
```javascript | ||
mongoQuery.projectionAdd(['field4', 'field5']); | ||
``` | ||
The default inclusion value is 1 and therefore the second argument is not necessary. | ||
Removes a projection criteria with key _field4_ | ||
```javascript | ||
mongoQuery.projectionRemove('field4'); | ||
``` | ||
It returns the current state of the mongoQuery object. | ||
## Getting the query | ||
@@ -120,0 +152,0 @@ |
17206
361
155