The MongoDB driver allows ObjectQL to store data in a MongoDB database (version 4.0+). It takes advantage of MongoDB's document model to store JSON fields natively and flexible schemas.
npm install @objectql/driver-mongo mongodbThe MongoDriver accepts connection parameters directly.
import { MongoDriver } from '@objectql/driver-mongo';
const driver = new MongoDriver({
url: 'mongodb://localhost:27017', // Connection string
dbName: 'my_app_db', // Database name
options: {
// Optional MongoDB client options
maxPoolSize: 10
}
});ObjectQL provides a unified id field across all database drivers for a consistent API experience.
- API Level: You always use
idin your application code (queries, filters, documents) - Database Level: MongoDB internally uses
_idas required by MongoDB conventions - Automatic Mapping: The driver transparently converts between
idand_id
Querying by ID:
// ✅ Use 'id' in queries - works consistently across all drivers
const query = {
filters: [['id', '=', '507f1f77bcf86cd799439011']]
};
const users = await app.find('users', query);Creating Documents:
// ✅ Use 'id' when creating - the driver maps it to '_id' internally
const newUser = await app.create('users', {
id: '507f1f77bcf86cd799439011', // Optional: specify custom ID
name: 'Alice',
email: 'alice@example.com'
});
// Result returned with 'id' field (not '_id')
console.log(newUser.id); // '507f1f77bcf86cd799439011'Finding by ID:
// ✅ Use 'id' parameter
const user = await app.findOne('users', '507f1f77bcf86cd799439011');
console.log(user.id); // Always 'id', never '_id'Sorting by ID:
const query = {
sort: [['id', 'desc']] // ✅ Use 'id' for sorting
};
const users = await app.find('users', query);Field Projection:
const query = {
fields: ['id', 'name', 'email'] // ✅ Use 'id' to select the ID field
};
const users = await app.find('users', query);
// Results contain 'id', not '_id'If you have existing code using _id, you have two options:
- Recommended: Migrate to
idfor consistency across drivers:
Before (Legacy):
// ❌ Old way - inconsistent with SQL drivers
const query = {
filters: [['_id', '=', '507f1f77bcf86cd799439011']],
sort: [['_id', 'desc']],
fields: ['_id', 'name']
};After (Recommended):
// ✅ New way - consistent across all drivers
const query = {
filters: [['id', '=', '507f1f77bcf86cd799439011']],
sort: [['id', 'desc']],
fields: ['id', 'name']
};- Backward Compatible Mode: Continue using
_idin queries (results still returnid)
The MongoDB driver fully supports _id in filters, sorting, and field projections for backward compatibility:
// ✅ This works - backward compatible
const query = {
filters: [['_id', '=', '507f1f77bcf86cd799439011']],
sort: [['_id', 'desc']],
fields: ['_id', 'name']
};
const users = await app.find('users', query);
// Results ALWAYS use 'id' regardless of query field name
console.log(users[0].id); // '507f1f77bcf86cd799439011'
console.log(users[0]._id); // undefinedKey Points:
- Queries accept both
idand_id(automatically mapped to MongoDB's_id) - Results always return
idfield (never_id) - Using
idis recommended for database portability
When creating documents without specifying an id, the driver automatically generates a string ID:
const newUser = await app.create('users', {
name: 'Bob',
// No id specified
});
// Driver generates a unique string ID (not ObjectId)
console.log(newUser.id); // e.g., '507f1f77bcf86cd799439011'The generated IDs are hexadecimal strings (24 characters) that maintain MongoDB's uniqueness guarantees without using ObjectId objects.
- Joins: While MongoDB supports
$lookup(which this driver uses for joins), complex relationships across many collections can be slower than SQL. - Transactions: Transactions are supported but require a MongoDB Replica Set deployment.