Methods
and(queries) → {Stack}
Logical AND query wrapper
Parameters:
Name | Type | Description |
---|---|---|
queries |
object | Query filter |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.and([
{
title: 'John'
},
{
age: 30
}
])
.find()
.then((result) => {
// filtered entries, where { title: 'John', age: 30 }
})
.catch((error) => {
// handle query errors
})
ascending(field) → {Stack}
Sorts the documents based on the 'sort' key
The sort function requires that the entire sort be able to complete within 32 megabytes.
When the sort option consumes more than 32 megabytes, MongoDB will return an error.
Parameters:
Name | Type | Description |
---|---|---|
field |
string | The field to sort in ascending order |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.ascending()
.find()
.then((result) => {
// result sorted in ascending manner with respect to 'published_at' field (by default)
})
.catch((error) => {
// handle query errors
})
asset(uid) → {Stack}
Query for a single asset
Parameters:
Name | Type | Description |
---|---|---|
uid |
string | Asset uid to be found, if not provided, by default returns the 1st element from assets. |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.asset()
.find()
.then((result) => {
// returns the asset based on its 'uid', if not provided, it would return the 1st asset found
})
.catch((error) => {
// handle query errors
})
assets() → {Stack}
Query for a set of assets
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.assets()
.find()
.then((result) => {
// returns assets filtered based on 'blog' content type
})
.catch((error) => {
// handle query errors
})
close()
Closes connection with mongodb
connect(overrides) → {object}
Establish connection to mongodb
Parameters:
Name | Type | Description |
---|---|---|
overrides |
object | Config overrides/mongodb specific config |
Returns:
Mongodb 'db' instance
- Type
- object
Example
Stack
.connect({overrides})
.then((result) => {
// mongodb connection object
// indexes will be created on the collection in the background if provided in config
})
.catch((error) => {
// handle query errors
})
containedIn(key, value) → {Stack}
Comparison $in query wrapper
Compares the field/key provided against the provided value.
Only documents that have value contained in the field/key provided are returned.
Check mongodb query here: https://docs.mongodb.com/manual/reference/operator/query/in/.
Res: https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing.
Comparison ordering
https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#bson-types-comparison-order
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.containedIn('emails', 'john.doe@some.com')
.find()
.then((result) => {
// filtered entries, where 'john.doe@some.com' exists in 'emails' field (array)
})
.catch((error) => {
// handle query errors
})
contentType(uid) → {Stack}
Content type to query on
Parameters:
Name | Type | Description |
---|---|---|
uid |
string | Content type uid |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.find()
.then((result) => {
// returns entries filtered based on 'blog' content type
})
.catch((error) => {
// handle query errors
})
contentTypes() → {Stack}
Query for a set of content type schemas
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentTypes()
.find()
.then((result) => {
// returns a set of content type schemas
})
.catch((error) => {
// handle query errors
})
count(query) → {object}
Parameters:
Name | Type | Description |
---|---|---|
query |
object | Optional query filter object |
Returns:
Returns count of the entries/asset's matched
- Type
- object
Example
Stack
.contentType('blog')
.entries()
.count()
.then((result) => {
// returns entries, without any of its assets Or references
})
.catch((error) => {
// handle query errors
})
descending(field) → {Stack}
Sorts the documents based on the 'sort' key
The sort function requires that the entire sort be able to complete within 32 megabytes.
When the sort option consumes more than 32 megabytes, MongoDB will return an error.
Parameters:
Name | Type | Description |
---|---|---|
field |
string | The field to sort in descending order |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.descending('title')
.find()
.then((result) => {
// result sorted in descending manner with respect to 'title' field
})
.catch((error) => {
// handle query errors
})
entries() → {Stack}
Query for a set of entries on a content type
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.find()
.then((result) => {
// returns entries filtered based on 'blog' content type
})
.catch((error) => {
// handle query errors
})
entry(uid) → {Stack}
Query for a single entry
Parameters:
Name | Type | Description |
---|---|---|
uid |
string | Entry uid to be found, if not provided, by default returns the 1st element in the content type. Useful for `singleton` content types |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entry()
.find()
.then((result) => {
// returns the entry based on its 'uid',
// if not provided, it would return the 1st entry found in 'blog' content type
})
.catch((error) => {
// handle query errors
})
except(fields) → {Stack}
Projections - returns fields except the ones passed here
Parameters:
Name | Type | Description |
---|---|---|
fields |
array | Array of 'fields', separated by dot ('.') notation for embedded document query |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.except(["title", "url", "links"])
.find()
.then((result) => {
// returns entries and projects all of their properties, except - ["title", "url", "links"]
})
.catch((error) => {
// handle query errors
})
excludeReferences() → {Stack}
Excludes all references of the entries being scanned.
Note: On calling this, assets will not be binded in the result being returned.
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.excludeReferences()
.find()
.then((result) => {
// returns entries, without any of its assets Or references
})
.catch((error) => {
// handle query errors
})
exists(key, value) → {Stack}
Element $exists query wrapper, checks if a field exists
Compares the field / key provided against the provided value.Only documents that have the field /
key specified are returned.
Check mongodb query here: https://docs.mongodb.com/manual/reference/operator/query/exists/.
Res: https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing.
Comparison ordering{
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.exists('emails')
.find()
.then((result) => {
// filtered entries, where 'emails' property exists
})
.catch((error) => {
// handle query errors
})
fetch(query) → {object}
Queries the db using the query built/passed. Returns a single entry/asset/content type object
Does all the processing, filtering, referencing after querying the DB
Parameters:
Name | Type | Description |
---|---|---|
query |
object | Optional query object, that overrides all the previously build queries |
Returns:
- Returns an object, that has been processed, filtered and referenced
- Type
- object
Example
Stack
.contentType('blog')
.entries()
.fetch()
find(query) → {object}
Queries the db using the query built/passed
Does all the processing, filtering, referencing after querying the DB
Parameters:
Name | Type | Description |
---|---|---|
query |
object | Optional query object, that overrides all the previously build queries |
Returns:
- Returns a objects, that have been processed, filtered and referenced
- Type
- object
Example
Stack
.contentType('blog')
.entries()
.find()
.then((result) => {
// returns blog content type's entries
})
.catch((error) => {
// handle query errors
})
findOne(query) → {object}
Queries the db using the query built/passed. Returns a single entry/asset/content type object
Does all the processing, filtering, referencing after querying the DB
Parameters:
Name | Type | Description |
---|---|---|
query |
object | Optional query object, that overrides all the previously build queries |
Returns:
- Returns an object, that has been processed, filtered and referenced
- Type
- object
Example
Stack
.contentType('blog')
.entries()
.findOne()
getQuery() → {Stack}
Returns the query build thusfar
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
const query = Stack
.contentType('blog')
.entries()
.getQuery()
// exposes details of the queries formed inside the SDK
greaterThan(key, value) → {Stack}
Comparison $gt query wrapper
Compares the field/key provided against the provided value.
Only documents that have greater value than the one provided are returned.
Check https://docs.mongodb.com/manual/reference/operator/query/gt/
and https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing for more info
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.greaterThan('age', 60)
.find()
.then((result) => {
// filtered entries, where { age > 60 }
})
.catch((error) => {
// handle query errors
})
greaterThanOrEqualTo(key, value) → {Stack}
Comparison $gte query wrapper
Compares the field/key provided against the provided value.
Only documents that have greater than or equal value than the one provided are returned.
Check https://docs.mongodb.com/manual/reference/operator/query/gte/ and
https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing for more info
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.greaterThanOrEqualTo('age', 60)
.find()
.then((result) => {
// filtered entries, where { age >= 60 }
})
.catch((error) => {
// handle query errors
})
include(fields) → {Stack}
Pass in reference field uids, that you want included in your result.
If you want all the references, use .includeReferences()
Parameters:
Name | Type | Description |
---|---|---|
fields |
object | An array of reference field uids |
Returns:
Returns 'this' instance (of Stack)
- Type
- Stack
Example
Stack.contentType('blog')
.entries()
.include(['related_blogs', 'authors.blogs']) // here related_blogs and authors.blogs are reference field uids
includeContentType() → {Stack}
Includes 'content_type' key in response, which is the content type schema of the entries filtered/scanned
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.includeContentType()
.find()
.then((result) => {
// returns entries, along with a 'content_type' property, which is 'blog' content type's schema
})
.catch((error) => {
// handle query errors
})
includeCount() → {Stack}
Includes 'count' key in response, which is the total count of the items being returned
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.includeCount()
.find()
.then((result) => {
// returns entries, along with a 'count' property, with the total count of entries being returned
})
.catch((error) => {
// handle query errors
})
includeReferences() → {Stack}
This method would return all the references of your queried entries (until depth 2)
Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter
Returns:
Returns 'this' instance (of Stack)
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.includeReferences(3)
language(code) → {Stack}
Locale to query on
Parameters:
Name | Type | Description |
---|---|---|
code |
string | Query locale's code |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.language('es-es')
.find()
.then((result) => {
// results in entries fetched from 'es-es' locale
// if not provided, defaults to the 1st locale provided in the 'locales' key, provided in config
})
.catch((error) => {
// handle query errors
})
lessThan(key, value) → {Stack}
Comparison $lt query wrapper
Compares the field/key provided against the provided value.
Only documents that have lower value than the one provided are returned.
Check https://docs.mongodb.com/manual/reference/operator/query/lt/
and https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing for more info
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.lessThan('age', 18)
.find()
.then((result) => {
// filtered entries, where { age < 18 }
})
.catch((error) => {
// handle query errors
})
lessThanOrEqualTo(key, value) → {Stack}
Comparison $lte query wrapper
Compares the field/key provided against the provided value.
Only documents that have lower or equal value than the one provided are returned.
Check https://docs.mongodb.com/manual/reference/operator/query/lte/
and https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing for more info
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.lessThanOrEqualTo('age', 18)
.find()
.then((result) => {
// filtered entries, where { age <= 18 }
})
.catch((error) => {
// handle query errors
})
limit(no) → {Stack}
Parameter - used to limit the total no of items returned/scanned
Defaults to 100 (internally, which is overridden)
Parameters:
Name | Type | Description |
---|---|---|
no |
number | Max count of the 'items' returned |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.limit(20)
.find()
.then((result) => {
// returns a maximum of 20 entries
// if not provided, by default - the limit specified in config is returned
})
.catch((error) => {
// handle query errors
})
notContainedIn(key, value) → {Stack}
Comparison $nin query wrapper
Compares the field/key provided against the provided value.
Only documents that have value not contained in the field/key provided are returned.
Check mongodb query here: https://docs.mongodb.com/manual/reference/operator/query/nin/.
Res: https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing.
Comparison ordering
https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#bson-types-comparison-order
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.notContainedIn('emails', 'john.doe@some.com')
.find()
.then((result) => {
// filtered entries, where 'john.doe@some.com' does not exist in 'emails' field (array)
})
.catch((error) => {
// handle query errors
})
notEqualTo(key, value) → {Stack}
Comparison $ne query wrapper
Compares the field/key provided against the provided value.
Only documents that have value not equals than the one provided are returned.
Check mongodb query here: https://docs.mongodb.com/manual/reference/operator/query/ne/.
Res: https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing.
Comparison ordering
https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#bson-types-comparison-order
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.notEqualTo('age', 25)
.find()
.then((result) => {
// filtered entries, where { age != 25 }
})
.catch((error) => {
// handle query errors
})
notExists(key, value) → {Stack}
Property $exists query wrapper, checks if a field does not exists
Compares the field/key provided against the provided value. Only documents that do not have the key are returned.
Check mongodb query here: https://docs.mongodb.com/manual/reference/operator/query/exists/.
Res: https://docs.mongodb.com/manual/reference/method/db.collection.find/#type-bracketing.
Comparison ordering{
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Field to compare against |
value |
* | Value to compare with |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.notExists('emails')
.find()
.then((result) => {
// filtered entries, where 'emails' property does not exist
})
.catch((error) => {
// handle query errors
})
only(fields) → {Stack}
Projections - returns only the fields passed here
Parameters:
Name | Type | Description |
---|---|---|
fields |
array | Array of 'fields', separated by dot ('.') notation for embedded document query |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.only(["title", "url", "links"])
.find()
.then((result) => {
// returns entries and projects only their - ["title", "url", "links"] properties
})
.catch((error) => {
// handle query errors
})
or(queries) → {Stack}
Logical OR query wrapper
Parameters:
Name | Type | Description |
---|---|---|
queries |
object | Query filter |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('')
.entries()
.or([
{
title: 'John'
},
{
title: 'Jane'
}
])
.find()
.then((result) => {
// filtered entries, where { title: 'John' } OR { title: 'Jane' }
})
.catch((error) => {
// handle query errors
})
query(queryObject) → {Stack}
Wrapper around a raw query wrapper
Parameters:
Name | Type | Description |
---|---|---|
queryObject |
object | Query filter |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.query({"group.heading": "Tab 1"})
.find()
.then((result) => {
// returns entries that have - {"group.heading": "Tab 1"}
})
.catch((error) => {
// handle query errors
})
queryReferences() → {Stack}
Wrapper, that allows querying on the entry's references.
Note: This is a slow method, since it scans all documents and fires the `reference`
query on them.Once the references are binded, the query object passed is used
for filtering
Use `.query()` filters to reduce the total no of documents being scanned
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.queryReferences({"authors.name": "John Doe"})
.find()
.then((result) => {
// returns entries, who's reference author's name equals "John Doe"
})
.catch((error) => {
// handle query errors
})
regex(field, pattern, options) → {Stack}
Raw regex to be applied on a field - wrapper
Parameters:
Name | Type | Description |
---|---|---|
field |
string | Field on which the regex is to be applied on |
pattern |
pattern | Regex pattern |
options |
options | Options to be applied while evaluating the regex |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.regex("name", "^J")
.find()
.then((result) => {
// returns entries who's name properties start with "J"
})
.catch((error) => {
// handle query errors
})
schema(uid) → {Stack}
Query for a single content type's schema
Parameters:
Name | Type | Description |
---|---|---|
uid |
string | Content type uid to be found, if not provided, by default returns the 1st element from content types |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.schema('blog')
.find()
.then((result) => {
// returns content 'blog' content type's schema
})
.catch((error) => {
// handle query errors
})
schemas() → {Stack}
Query for a set of content type schemas
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.schemas()
.find()
.then((result) => {
// returns a set of content type schemas
})
.catch((error) => {
// handle query errors
})
skip(no) → {Stack}
Parameter - used to skip initial no of items scanned
Defaults to 0 (internally, which is overridden)
Parameters:
Name | Type | Description |
---|---|---|
no |
number | Min count of the 'items' to be scanned |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.skip(10)
.find()
.then((result) => {
// returnes entries, after first skipping 20 entries of 'blog' content type
// if not provided, by default - the skip value provided in config is considered
})
.catch((error) => {
// handle query errors
})
Stack(config, db) → {Stack}
Initialize Stack instance
Parameters:
Name | Type | Description |
---|---|---|
config |
object | Stack config |
db |
object | Existing db connection |
Returns:
- Returns an instance of `Stack`
- Type
- Stack
tags(values) → {Stack}
Match entries that match a specific tags
Parameters:
Name | Type | Description |
---|---|---|
values |
array | Array of tag values |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.tags(["new", "fresh"])
.find()
.then((result) => {
// returns entries filtered based on their tag fields
})
.catch((error) => {
// handle query errors
})
where(expr) → {Stack}
Pass JS expression or a full function to the query system
Use the $where operator to pass either a string containing a JavaScript expression or a full JavaScript
function to the query system.
The $where provides greater flexibility, but requires that the database processes the JavaScript expression or
function for each document in the collection.
Reference the document in the JavaScript expression or function using either this or obj.
Only apply the $where query operator to top-level documents.
The $where query operator will not work inside a nested document, for instance, in an $elemMatch query.
Ref. - https://docs.mongodb.com/manual/reference/operator/query/where/index.html
Parameters:
Name | Type | Description |
---|---|---|
expr |
* | Pass either a string containing a JavaScript expression or a full JavaScript function to the query system. |
Returns:
Returns an instance of 'stack'
- Type
- Stack
Example
Stack
.contentType('blog')
.entries()
.where(function() {
return (hex_md5(this.name) === "9b53e667f30cd329dca1ec9e6a83e994")
})
.find()
.then((result) => {
// returns entries filtered based on the $where condition provided
})
.catch((error) => {
// handle query errors
})