Query Guide
KeraDB supports MongoDB-compatible query operators for powerful data retrieval and manipulation.
Basic Queries
Exact Match
- Node.js
- Python
// Find by single field
const user = await users.findOne({ name: 'Alice' });
// Find by multiple fields
const user = await users.findOne({
name: 'Alice',
age: 28
});
// Find all matching documents
const developers = await users.find({ role: 'developer' }).toArray();
# Find by single field
user = users.find_one({'name': 'Alice'})
# Find by multiple fields
user = users.find_one({
'name': 'Alice',
'age': 28
})
# Find all matching documents
developers = list(users.find({'role': 'developer'}))
Comparison Operators
$eq (Equal)
Matches values that are equal to a specified value.
- Node.js
- Python
const users = await users.find({ age: { $eq: 25 } }).toArray();
// Same as: { age: 25 }
users_list = list(users.find({'age': {'$eq': 25}}))
# Same as: {'age': 25}
$ne (Not Equal)
Matches values that are not equal to a specified value.
- Node.js
- Python
const users = await users.find({ role: { $ne: 'admin' } }).toArray();
users_list = list(users.find({'role': {'$ne': 'admin'}}))
$gt, $gte (Greater Than, Greater Than or Equal)
- Node.js
- Python
// Greater than
const adults = await users.find({ age: { $gt: 18 } }).toArray();
// Greater than or equal
const voters = await users.find({ age: { $gte: 18 } }).toArray();
# Greater than
adults = list(users.find({'age': {'$gt': 18}}))
# Greater than or equal
voters = list(users.find({'age': {'$gte': 18}}))
$lt, $lte (Less Than, Less Than or Equal)
- Node.js
- Python
// Less than
const minors = await users.find({ age: { $lt: 18 } }).toArray();
// Less than or equal
const eligible = await users.find({ age: { $lte: 65 } }).toArray();
# Less than
minors = list(users.find({'age': {'$lt': 18}}))
# Less than or equal
eligible = list(users.find({'age': {'$lte': 65}}))
$in (In Array)
Matches any of the values specified in an array.
- Node.js
- Python
const staff = await users.find({
role: { $in: ['admin', 'moderator', 'editor'] }
}).toArray();
staff = list(users.find({
'role': {'$in': ['admin', 'moderator', 'editor']}
}))
$nin (Not In Array)
Matches none of the values specified in an array.
- Node.js
- Python
const regularUsers = await users.find({
role: { $nin: ['admin', 'moderator'] }
}).toArray();
regular_users = list(users.find({
'role': {'$nin': ['admin', 'moderator']}
}))
Logical Operators
$and
Joins query clauses with a logical AND.
- Node.js
- Python
const result = await users.find({
$and: [
{ age: { $gte: 18 } },
{ role: 'developer' },
{ active: true }
]
}).toArray();
result = list(users.find({
'$and': [
{'age': {'$gte': 18}},
{'role': 'developer'},
{'active': True}
]
}))
$or
Joins query clauses with a logical OR.
- Node.js
- Python
const result = await users.find({
$or: [
{ role: 'admin' },
{ role: 'moderator' },
{ permissions: 'superuser' }
]
}).toArray();
result = list(users.find({
'$or': [
{'role': 'admin'},
{'role': 'moderator'},
{'permissions': 'superuser'}
]
}))
$not
Inverts the effect of a query expression.
- Node.js
- Python
const result = await users.find({
age: { $not: { $lt: 18 } }
}).toArray();
result = list(users.find({
'age': {'$not': {'$lt': 18}}
}))
$nor
Joins query clauses with a logical NOR.
- Node.js
- Python
const result = await users.find({
$nor: [
{ role: 'admin' },
{ banned: true }
]
}).toArray();
result = list(users.find({
'$nor': [
{'role': 'admin'},
{'banned': True}
]
}))
Element Operators
$exists
Matches documents that have the specified field.
- Node.js
- Python
// Find users with email field
const withEmail = await users.find({
email: { $exists: true }
}).toArray();
// Find users without phone field
const withoutPhone = await users.find({
phone: { $exists: false }
}).toArray();
# Find users with email field
with_email = list(users.find({
'email': {'$exists': True}
}))
# Find users without phone field
without_phone = list(users.find({
'phone': {'$exists': False}
}))
$type
Selects documents if a field is of the specified type.
- Node.js
- Python
const stringIds = await users.find({
_id: { $type: 'string' }
}).toArray();
const numericAges = await users.find({
age: { $type: 'number' }
}).toArray();
string_ids = list(users.find({
'_id': {'$type': 'string'}
}))
numeric_ages = list(users.find({
'age': {'$type': 'number'}
}))
Array Operators
$all
Matches arrays that contain all elements specified in the query.
- Node.js
- Python
const experts = await users.find({
skills: { $all: ['JavaScript', 'Python', 'Docker'] }
}).toArray();
experts = list(users.find({
'skills': {'$all': ['Python', 'JavaScript', 'Docker']}
}))
$elemMatch
Matches documents that contain an array field with at least one element matching all criteria.
- Node.js
- Python
const results = await orders.find({
items: {
$elemMatch: {
price: { $gte: 100 },
quantity: { $gt: 1 }
}
}
}).toArray();
results = list(orders.find({
'items': {
'$elemMatch': {
'price': {'$gte': 100},
'quantity': {'$gt': 1}
}
}
}))
$size
Matches arrays with a specified number of elements.
- Node.js
- Python
const threeSkills = await users.find({
skills: { $size: 3 }
}).toArray();
three_skills = list(users.find({
'skills': {'$size': 3}
}))
String Operators
$regex
Provides regular expression pattern matching.
- Node.js
- Python
// Case-insensitive search
const gmailUsers = await users.find({
email: { $regex: /gmail\.com$/i }
}).toArray();
// Find names starting with 'A'
const aNames = await users.find({
name: { $regex: /^A/ }
}).toArray();
# Case-insensitive search
gmail_users = list(users.find({
'email': {'$regex': r'gmail\.com$', '$options': 'i'}
}))
# Find names starting with 'A'
a_names = list(users.find({
'name': {'$regex': r'^A'}
}))
Update Operators
$set
Sets the value of a field.
- Node.js
- Python
await users.updateOne(
{ email: 'alice@example.com' },
{ $set: { age: 29, role: 'senior developer' } }
);
users.update_one(
{'email': 'alice@example.com'},
{'$set': {'age': 29, 'role': 'senior developer'}}
)
$unset
Removes a field from a document.
- Node.js
- Python
await users.updateOne(
{ email: 'bob@example.com' },
{ $unset: { tempField: '' } }
);
users.update_one(
{'email': 'bob@example.com'},
{'$unset': {'temp_field': ''}}
)
$inc
Increments the value of a field by a specified amount.
- Node.js
- Python
await users.updateOne(
{ email: 'alice@example.com' },
{ $inc: { loginCount: 1, points: 10 } }
);
users.update_one(
{'email': 'alice@example.com'},
{'$inc': {'login_count': 1, 'points': 10}}
)
$push
Adds an element to an array.
- Node.js
- Python
await users.updateOne(
{ email: 'alice@example.com' },
{ $push: { skills: 'TypeScript' } }
);
users.update_one(
{'email': 'alice@example.com'},
{'$push': {'skills': 'TypeScript'}}
)
$pull
Removes all array elements that match a specified query.
- Node.js
- Python
await users.updateOne(
{ email: 'alice@example.com' },
{ $pull: { skills: 'jQuery' } }
);
users.update_one(
{'email': 'alice@example.com'},
{'$pull': {'skills': 'jQuery'}}
)
$addToSet
Adds elements to an array only if they do not already exist.
- Node.js
- Python
await users.updateOne(
{ email: 'alice@example.com' },
{ $addToSet: { skills: 'React' } }
);
users.update_one(
{'email': 'alice@example.com'},
{'$addToSet': {'skills': 'React'}}
)
Query Modifiers
Sorting
- Node.js
- Python
// Sort ascending
const users = await users.find().sort({ age: 1 }).toArray();
// Sort descending
const users = await users.find().sort({ age: -1 }).toArray();
// Multiple sort fields
const users = await users.find()
.sort({ role: 1, age: -1 })
.toArray();
# Sort ascending
users_list = list(users.find().sort('age', 1))
# Sort descending
users_list = list(users.find().sort('age', -1))
# Multiple sort fields
users_list = list(users.find().sort([('role', 1), ('age', -1)]))
Limiting Results
- Node.js
- Python
// Get first 10 results
const users = await users.find().limit(10).toArray();
// Skip and limit (pagination)
const page2 = await users.find()
.skip(10)
.limit(10)
.toArray();
# Get first 10 results
users_list = list(users.find().limit(10))
# Skip and limit (pagination)
page2 = list(users.find().skip(10).limit(10))
Field Projection
Select which fields to return.
- Node.js
- Python
// Include only specific fields
const users = await users.find({}, {
name: 1,
email: 1
}).toArray();
// Exclude specific fields
const users = await users.find({}, {
password: 0,
internalId: 0
}).toArray();
# Include only specific fields
users_list = list(users.find({}, {
'name': 1,
'email': 1
}))
# Exclude specific fields
users_list = list(users.find({}, {
'password': 0,
'internal_id': 0
}))
Complex Query Examples
Combining Multiple Operators
- Node.js
- Python
const result = await users.find({
$and: [
{ age: { $gte: 18, $lte: 65 } },
{
$or: [
{ role: 'developer' },
{ role: 'designer' }
]
},
{ skills: { $all: ['JavaScript', 'React'] } },
{ email: { $exists: true } }
]
}).sort({ createdAt: -1 }).limit(20).toArray();
result = list(users.find({
'$and': [
{'age': {'$gte': 18, '$lte': 65}},
{
'$or': [
{'role': 'developer'},
{'role': 'designer'}
]
},
{'skills': {'$all': ['JavaScript', 'React']}},
{'email': {'$exists': True}}
]
}).sort('created_at', -1).limit(20))
Next Steps
- Examples - See practical query examples
- Getting Started - SDK documentation with core concepts
- Contributing - Help improve KeraDB