Node.js Backend Development Bootcamp · Pelajaran

Melakukan Kueri & Memfilter Data dengan Mongoose

Pelajari lebih dari sekadar CRUD dasar dan cara membangun kueri yang andal di Mongoose menggunakan filter, operator, pengurutan, penomoran halaman, dan proyeksi.

Pelajaran 4 dari 413 langkah

Melakukan Kueri & Memfilter Data dengan Mongoose adalah pelajaran Node.js Backend Development Bootcamp gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Node.js Backend Development Bootcamp, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Node.js Backend Development Bootcamp mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

Beyond Find-All

Fetching every document with Model.find() works for tiny collections, but real apps need to filter, sort, limit, and shape their results.

Mongoose gives you a fluent query API that mirrors MongoDB's query language.

const users = await User.find({ active: true });

Filtering by Field

Pass an object to find() to match documents by exact field values. Multiple keys are combined with logical AND.

const admins = await User.find({ role: 'admin', active: true });

Comparison Operators

MongoDB operators start with $. The most common comparisons are:

  • $gt / $gte greater than (or equal)
  • $lt / $lte less than (or equal)
  • $ne not equal
const adults = await User.find({ age: { $gte: 18 } });

Matching Multiple Values

Use $in to match any value from a list, and $nin to exclude a list. This is cleaner than chaining many OR conditions.

const team = await User.find({ role: { $in: ['admin', 'editor'] } });

Logical OR

The $or operator takes an array of conditions; a document matches if any one is true.

const results = await User.find({
  $or: [{ city: 'Berlin' }, { city: 'Paris' }]
});

Sorting Results

Chain .sort() to order results. Use 1 for ascending and -1 for descending. You can sort by multiple fields.

const recent = await User.find().sort({ createdAt: -1, name: 1 });

Limiting and Skipping

Pagination relies on .limit() (max documents) and .skip() (how many to bypass). For page 2 with 10 per page, skip 10 and limit 10.

const page = 2, perPage = 10;
const items = await User.find()
  .skip((page - 1) * perPage)
  .limit(perPage);

Selecting Specific Fields

Use .select() (projection) to return only the fields you need. Prefix a field with - to exclude it. This reduces payload size and hides sensitive data.

const safe = await User.find().select('name email -_id');
const noPass = await User.find().select('-password');

Finding a Single Document

findOne() returns the first match or null. findById() is a shortcut for matching by _id.

const user = await User.findOne({ email: 'a@b.com' });
const byId = await User.findById('652f1c...');

Counting Documents

To know how many documents match a filter without fetching them all, use countDocuments(). This is essential for showing total pages in pagination.

const total = await User.countDocuments({ active: true });
console.log('Active users:', total);

Text & Regex Search

For partial matches, use a regular expression. The $regex operator with the i option makes a case-insensitive search.

const matches = await User.find({
  name: { $regex: 'an', $options: 'i' }
});

Quick Check

Test your Mongoose querying skills.

Recap

You can now build rich Mongoose queries:

  • Filter by field and combine with operators like $gte, $in, $or
  • Order results with .sort()
  • Paginate with .skip() and .limit()
  • Shape output with .select()
  • Count matches with countDocuments()

These tools turn a basic data layer into a flexible, production-ready API.

Gratis untuk memulai

Belajar JavaScript dengan tutor AI — gratis

Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.

Kursus
22
Pelajaran
92

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Melakukan Kueri & Memfilter Data dengan Mongoose” gratis?

Ya — teks lengkap “Melakukan Kueri & Memfilter Data dengan Mongoose” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Node.js Backend Development Bootcamp, upgrade ke CoddyKit PRO. Kursus Node.js Backend Development Bootcamp mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Melakukan Kueri & Memfilter Data dengan Mongoose”?

Pelajari lebih dari sekadar CRUD dasar dan cara membangun kueri yang andal di Mongoose menggunakan filter, operator, pengurutan, penomoran halaman, dan proyeksi. Kamu berlatih Node.js Backend Development Bootcamp dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Node.js Backend Development Bootcamp?

Tidak diperlukan pengalaman sebelumnya. Node.js Backend Development Bootcamp di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Melakukan Kueri & Memfilter Data dengan Mongoose” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Node.js Backend Development Bootcamp ini?

Ya. Setiap pelajaran Node.js Backend Development Bootcamp menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Menghubungkan Node.js ke MongoDB
  2. ODM Mongoose untuk Pemodelan Data
  3. Operasi CRUD dengan Mongoose
  4. Melakukan Kueri & Memfilter Data dengan Mongoose
← Kembali ke Node.js Backend Development Bootcamp