Skip to main content
Version: v1.1.x

Read Data

Read

select(columns)

Select specific columns to be displayed in the query results.

import User from "./yourPath/User";

const users = await User.select("name").select("age").get();

Also, you can pass a list of column names to select some columns.

import User from "./yourPath/User";

const users = await User.select(["name", "age"]).get();

exclude(columns)

Exclude specific columns from being displayed in the query results.

import User from "./yourPath/User";

const users = await User.exclude("name").exclude("age").get();

Also, you can pass a list of column names to exclude some columns.

import User from "./yourPath/User";

const users = await User.exclude(["name", "age"]).get();

all()

The all method will retrieve all of the documents from the model's associated database collection.

import User from "./yourPath/User";

const users = await User.all();

get(columns)

Get method return the documents matching the query criteria. The get method will return an array.

import User from "./yourPath/User";

const users = await User.get();

Also, you can pass a column name to select specific columns.

import User from "./yourPath/User";

const users = await User.get("name");

Or pass list a column names to select some columns.

import User from "./yourPath/User";

const users = await User.get(["name", "age"]);

paginate(page, limit)

You can use the paginate method to paginate the query results. It takes two parameters: page: int and limit: int.

The paginate method will return an object with data and meta properties.

import User from "./yourPath/User";

const users = await User.paginate(1, 10);

first(columns)

Get the first document matching the query criteria. The first method will return an object.

import User from "./yourPath/User";

const user = await User.where("_id", "65ab7e3d05d58a1ad246ee87").first();

Also, you can pass a column name to select specific columns.

import User from "./yourPath/User";

const user = await User.where("_id", "65ab7e3d05d58a1ad246ee87").first("name");

Or pass list a column names to select some columns.

import User from "./yourPath/User";

const user = await User.where("_id", "65ab7e3d05d58a1ad246ee87").first([
"name",
"age",
]);

find(id)

Find a document by its ID. The find method will return an object.

import User from "./yourPath/User";
import { ObjectId } from "mongodb";

const user = await User.find(new ObjectId("65ab7e3d05d58a1ad246ee87"));

Also, you can pass a string id.

import User from "./yourPath/User";

const user = await User.find("65ab7e3d05d58a1ad246ee87");

pluck(column)

Retrieve the values of a specific column from the query results.

import User from "./yourPath/User";

const users = await User.pluck("name").get();

limit(value)

Limit the number of documents to be returned by the query.

import User from "./yourPath/User";

const users = await User.limit(10).get();

take(value)

Alias for the limit method.

import User from "./yourPath/User";

const users = await User.take(10).get();

offset(value)

Set an offset for the query results.

import User from "./yourPath/User";

const users = await User.offset(10).get();

Also, you can use offset and limit methods to paginate your query results.

import User from "./yourPath/User";

const users = await User.offset(10).limit(10).get();

skip(value)

Alias for the offset method.

import User from "./yourPath/User";

const users = await User.skip(10).get();

Also, you can use skip and limit methods to paginate your query results.

import User from "./yourPath/User";

const users = await User.skip(10).limit(10).get();

where(column, operator, value)

Add a WHERE clause to the query.

The where method takes three parameters: column: str, operator: str, and value: any.

import User from "./yourPath/User";

const users = await User.where("age", "eq", 17).get();

Also, you can use SQL’s comparation operators. For more detail, you can see it in the comparison operator table.

import User from "./yourPath/User";

const users = await User.where("age", "=", 17).get();

If you just pass two parameters, the second parameter will be assumed to be a value, and the operator is eq or =.

import User from "./yourPath/User";

const users = await User.where("age", 17).get();

orWhere(column, operator, value)

Add an OR WHERE clause to the query. You can combine this method with the where method.

import User from "./yourPath/User";

const users = await User.where("age", 17).orWhere("name", "like", "udin").get();

whereIn(column, values)

Add a WHERE IN clause to the query. The whereIn method takes two parameters: column: str, and values: any[].

import User from "./yourPath/User";

const users = await User.whereIn("age", [17, 20]).get();

orWhereIn(column, values)

Add an OR WHERE IN clause to the query. You can combine this method with the whereIn method.

import User from "./yourPath/User";

const users = await User.whereIn("age", [17, 20])
.orWhereIn("name", ["Udin", "Kosasih"])
.get();

whereNotIn(column, values)

Add an WHERE NOT IN clause to the query. The whereNotIn method takes two parameters: column: str, and values: any[].

import User from "./yourPath/User";

const users = await User.whereNotIn("age", [17, 20]).get();

orWhereNotIn(column, values)

Add an OR WHERE NOT IN clause to the query. You can combine this method with the whereNotIn method.

import User from "./yourPath/User";

const users = await User.whereNotIn("age", [17, 20])
.orWhereNotIn("name", ["Udin", "Kosasih"])
.get();

whereBetween(column, values)

Add a WHERE BETWEEN clause to the query. The whereBetween method takes two parameters: column: str and values: int[].

import User from "./yourPath/User";

const users = await User.whereBetween("age", [17, 20]).get();

orWhereBetween(column, values)

Add an OR WHERE BETWEEN clause to the query. You can combine this method with the whereBetween method.

import User from "./yourPath/User";

const users = await User.whereBetween("age", [15, 20])
.orWhereBetween("age", [50, 70])
.get();

orderBy(column, direction?, isSensitive?)

Sort the query results by a specific column. The orderBy method takes three parameters: column: str, direction: str, and isSensitive: bool.

The direction parameter is asc or desc. If you just pass one parameter, the direction is asc.

import User from "./yourPath/User";

const users = await User.orderBy("age").get();

Alse, you can set the isSensitive parameter true to sort string values.

import User from "./yourPath/User";

const users = await User.orderBy("name", "desc", true).get();

groupBy(column)

Group the query results by specific column.

import User from "./yourPath/User";

const users = await User.groupBy("age").get();

min(column)

Retrieve the minimum value of a specific column. The min method will return a number.

import Product from "./yourPath/Product";

const price = await Product.min("price");

max(column)

Retrieve the maximum value of a specific column. The max method will return a number.

import Product from "./yourPath/Product";

const price = await Product.max("price");

sum(column)

Calculate the sum of values in a specific column. The sum method will return a number.

import Product from "./yourPath/Product";

const price = await Product.sum("price");

avg(column)

Calculate the average value of a specific column. The avg method will return a number.

import Product from "./yourPath/Product";

const price = await Product.avg("price");

count()

Count the number of documents matching the query criteria. The count method will return a number.

import Product from "./yourPath/Product";

const products = await Product.where("price", ">=", 10000).count();

Support us

Mongoloquent is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.

Sponsors