This section is only relevant if you're interested in building your own Conduit modules.
Query Operations
The database module allows you to conveniently query your data without the need of writing complex SQL queries.
Schemas can be manipulated through model controller objects automatically generated by the module itself.
Suppose you're building a module that keeps track of superheroes and villains.
Having registered your schemas, this is how you'd query for villains facing off against Mammothman
, returning their name
, antagonist
and universe
fields.
You'll get up to 3 results, containing the 7th, 8th and 9th entries matching the above criteria due to the specified pagination (skip
, limit
).
Your villains are going to be sorted in a descending order based on their name
field.
Through the use of populate
, the antagonist
relation field ids are going to be replaced by the entire Antagonist entry objects in reference.
const mammothVillains = Villains.getInstance().findMany(
{ antagonist: "Mammothman" }, // query
"name antagonist universe", // select
6, // skip
3, // limit
"-name", // sort
"antagonist", // populate
)
Throughout the examples listed below, we'll be utilizing SchemaName
to represent our model controllers.
Comparison Operators
$in
The $in operator selects the documents where the value of a field equals any value in the specified array.
To specify an $in expression, use the following prototype:
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
const documents = SchemaName.getInstance().findMany({ _id: { $in: ids})
Where ids is an array of object ids.
$contains
In mongoDB $contains and $in operator does the same thing.
However in SQL DBs $contains differs from $in operator.
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
$nin
- Selects the documents when a field value is not inside an
array
. - Selects the documents when the field does not exist.
{ field: { $nin: [ <value1>, <value2> ... <valueN> ] } }
const documents = SchemaName.getInstance().findMany({ id : { $nin : ids} })
Where ids is an array of object ids.
$eq
Returns the documents where the value of a field is equal with a specified value.
{ <field>: { $eq: <value> } }
or
{ <field>: <value> }
const documents = SchemaName.getInstance().findMany({ provider: { $eq: "conduit"})
$ne
Returns the documents where the value of a field isn't equal with a specified value.
{ <field>: { $eq: <value> } }
const documents = SchemaName.getInstance().findMany({ provider: { $ne: "conduit" })
$lt
Returns the documents where the value of a field is less than a specified value.
{ <field>: { $lt: <value> } }
const documents = SchemaName.getInstance().findMany({ age: { $lt: 18 })
$lte
Returns the documents where the value of a field is less or equal to a specified value.
{ <field>: { $lte: <value> } }
const documents = SchemaName.getInstance().findMany({ age: { $lte: 18 })
$gt
Returns the documents where the value of a field is greater than a specified value.
{ <field>: { $gt: <value> } }
const documents = SchemaName.getInstance().findMany({ age: { $gt: 18 })
$gte
Returns the documents where the value of a field is greater or equal to a specified value.
{ <field>: { $gte: <value> } }
const documents = SchemaName.getInstance().findMany({ age: { $gte: 18 })
$regex
Returns the documents where the value of a field matches with the specified regex.
{ <field>: { $regex: <regex> } }
const documents = SchemaName.getInstance().findMany({ name: { $regex: '.*conduit.*' })
Logical Operators
$or
Performs logical OR operation on two or more expressions and returns the documents that satisfy at least one.
{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
const documents = SchemaName.getInstance().findMany( { $or: [ { price: { $lt: 20 } }, { expires: 10 } ] } )
$and
Performs logical AND operation on two or more expressions and returns the documents that satisfy all the expressions.
{ $and: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
const documents = SchemaName.getInstance().findMany( { $and: [ { price: { $lt: 20 } }, { expires: 10 } ] } )
$not
Performs logical NOT operation on an <operator expression>
s and returns the documents do not match the expression.
{ field: { $not: { <operator-expression> } } }
const documents = SchemaName.getInstance().findMany( { price: { $not: { $lte: 5 } } } )
Query parameters
Select
select = "<fieldA> <fieldB>"
Skip
Skip value defines how many documents will be skipped.
skip = <int>
Limit
Limit value defines how many documents you are gonna fetch.
limit = <int>
Sort
Database module supports descending and ascending sorting order. Skip value defines how many documents will be skipped.
sort = "<fieldName>" // ascending order
sort = "-<fieldName>" // descending order
Populate
You may choose either one of the following syntax formats:
populate = ["<objectFieldA>", "<objectFieldB>"]
populate = "<objectFieldA>,<objectFieldB>"