Basic Queries

This tutorial guides you through the fundamental ways to query your data using our system. We'll cover simple retrieval, filtering, and sorting.

1. Retrieving All Records

To get all records from a specific collection, you can use the getAll method. This is useful for initial exploration or when you need the entire dataset.


// Assuming 'db' is your database instance
db.collection('users').getAll()
  .then(users => {
    console.log('All users:', users);
  })
  .catch(error => {
    console.error('Error fetching users:', error);
  });
        

2. Filtering Records with find

The find method allows you to retrieve records that match specific criteria. You provide a query object to specify the conditions.

2.1. Simple Equality Match

Find all users whose status is 'active':


db.collection('users').find({ status: 'active' })
  .then(activeUsers => {
    console.log('Active users:', activeUsers);
  })
  .catch(error => {
    console.error('Error finding active users:', error);
  });
        

2.2. Using Comparison Operators

You can use operators like $gt (greater than), $lt (less than), $eq (equal to), $ne (not equal to), etc.

Find all products with a price greater than 50:


db.collection('products').find({ price: { $gt: 50 } })
  .then(expensiveProducts => {
    console.log('Expensive products:', expensiveProducts);
  })
  .catch(error => {
    console.error('Error finding expensive products:', error);
  });
        

For a full list of comparison operators, refer to the Query Operators Reference.

2.3. Combining Multiple Conditions

You can combine conditions using logical operators like $and and $or.

Find all users who are 'active' AND older than 30:


db.collection('users').find({
  $and: [
    { status: 'active' },
    { age: { $gt: 30 } }
  ]
})
  .then(results => {
    console.log('Active users over 30:', results);
  })
  .catch(error => {
    console.error('Error finding combined conditions:', error);
  });
        

Note: When you provide multiple key-value pairs in a query object without explicit logical operators, they are implicitly combined with $and. For example, { status: 'active', age: { $gt: 30 } } is equivalent to the $and example above.

3. Sorting Results with sort

The sort method allows you to order your query results. It takes an object where keys are the fields to sort by, and values are 1 for ascending or -1 for descending.

Find all users, sorted by age in ascending order:


db.collection('users').find().sort({ age: 1 })
  .then(sortedUsers => {
    console.log('Users sorted by age (ascending):', sortedUsers);
  })
  .catch(error => {
    console.error('Error sorting users:', error);
  });
        

Sort by registration date descending, then by username ascending:


db.collection('users').find().sort({ registeredDate: -1, username: 1 })
  .then(sortedUsers => {
    console.log('Users sorted by date (desc) and username (asc):', sortedUsers);
  })
  .catch(error => {
    console.error('Error sorting users:', error);
  });
        

4. Limiting and Skipping Results

You can control the number of results returned and skip a certain number of records using limit and skip methods.

Get the first 10 active users, sorted by their creation date:


db.collection('users').find({ status: 'active' })
  .sort({ createdAt: -1 })
  .limit(10)
  .then(latestActiveUsers => {
    console.log('Top 10 latest active users:', latestActiveUsers);
  })
  .catch(error => {
    console.error('Error limiting results:', error);
  });
        

Get users 11 through 20 (skip 10, limit 10):


db.collection('users').find()
  .skip(10)
  .limit(10)
  .then(nextPageUsers => {
    console.log('Users page 2:', nextPageUsers);
  })
  .catch(error => {
    console.error('Error skipping results:', error);
  });
        

Tip: It's often best practice to sort before applying limit and skip to ensure consistent pagination.

Next Steps

You've now learned the basics of querying data. Continue to the Advanced Operations tutorial to explore more complex data manipulation techniques.