CRUD Operations
CRUD stands for Create, Read, Update, Delete – the four basic operations every database must support. file-json-db provides both synchronous and asynchronous versions of each.
Create – Insert
insert(document) sync
Insert a single document. Returns the inserted document (with generated ID if applicable).
const user = users.insert({
name: 'Alice',
email: 'alice@example.com',
age: 28
});
console.log(user._id); // → 1 (if autoId is true)
insertAsync(document) async
const user = await users.insertAsync({
name: 'Bob',
email: 'bob@example.com'
});
Async inserts go through the collection’s internal lock/queue, so concurrent async writes on the same collection are serialized.
insertMany(documents) / insertManyAsync(documents)
Insert an array of documents in one go. Returns the array of inserted documents.
const inserted = users.insertMany([
{ name: 'Carol', email: 'carol@example.com' },
{ name: 'Dave', email: 'dave@example.com' }
]);
// Async version
const inserted2 = await users.insertManyAsync([
{ name: 'Eve', email: 'eve@example.com' }
]);
insertMany respects maxRecords / maxPartSize
and will create new part files as needed while inserting the batch.
Read – Find
findOne(query) / findOneAsync(query)
Return the first matching document, or null if none match.
const user = users.findOne({ email: 'alice@example.com' });
const user2 = await users.findOneAsync({ name: 'Bob' });
find(query) / findAsync(query)
Return a QueryBuilder.
You must call .toArray(), .first() or .count()
to get the actual results.
// All adults
const adults = users.find({ age: { $gte: 18 } }).toArray();
// First match only
const first = users.find({ active: true }).first();
// Just the count
const total = users.find({}).count();
// With limit / sort
const page = users.find({ active: true })
.sort({ name: 1 })
.skip(20)
.limit(10)
.toArray();
Update
updateOne(filter, update) / updateOneAsync
Update the first document that matches the filter. Returns the updated document or null.
users.updateOne(
{ email: 'alice@example.com' },
{ $set: { age: 29 }, $push: { tags: 'verified' } }
);
updateMany(filter, update) / updateManyAsync
Update every document that matches. Returns an array of the updated documents.
const updated = users.updateMany(
{ active: false },
{ $set: { status: 'archived' } }
);
console.log('Updated', updated.length, 'documents');
Full details of update operators ($set, $inc, $push, …)
are on the Update Operators page.
updateFunc / updateFuncAsync
For complex logic you can supply a function that receives the document and returns the new version (or mutates it):
users.updateFunc(
{ role: 'member' },
(doc) => {
doc.points = (doc.points || 0) + 10;
doc.lastBonus = new Date().toISOString();
return doc;
},
{ multi: true } // update all matches
);
Delete
deleteOne(filter) / deleteOneAsync
Delete the first matching document. Returns the deleted document or null.
const removed = users.deleteOne({ email: 'bob@example.com' });
deleteMany(filter) / deleteManyAsync
Delete every matching document. Returns an array of the deleted documents.
const removed = users.deleteMany({ active: false });
console.log('Deleted', removed.length, 'inactive users');
Summary of methods
| Operation | Sync | Async |
|---|---|---|
| Insert one | insert | insertAsync |
| Insert many | insertMany | insertManyAsync |
| Find one | findOne | findOneAsync |
| Find many | find | findAsync |
| Update one | updateOne | updateOneAsync |
| Update many | updateMany | updateManyAsync |
| Custom update | updateFunc | updateFuncAsync |
| Delete one | deleteOne | deleteOneAsync |
| Delete many | deleteMany | deleteManyAsync |