Mongfaux is a reverse engineering of the mongodb interface. It saves the data as flat JSON files for fast prototyping and/or a Git-able datastore.
-
Mongfaux is <600 lines of code and only attempts to replicate a small, heavily used, portion of the API. You should consider using a real database if you need more.
-
Mongfaux uses a simple string for the auto-generated "_id" field. I would suggest ignoring _id and creating your own "id" field using something like shortid. Alternatively you can manually set your own _id with require('mongodb').ObjectID.
-
Doesn't have your back with things like enforcing unique keys. Inserting two documents with the same "_id" will pass through no problem.
-
Does not produce very helpful errors
-
Very little consideration has been given to performance. Use mongoDB if that's a priority!
-
Callbacks didn't make the cut, use promises
npm install mongfaux
const db = require('mongfaux');
await db.collection('book').insertOne({author: 'foobar', title: 'Mongfaux'});
let books = await db.collection('book').find({}).toArray();
//.json files saved in process.cwd()/mongfaux/ by default
const Mf = require('mongfaux');
const db1 = Mf.instance();
const db2 = Mf.instance();
db1.name('db1');
db2.name('db2');
//mongfaux/
//--db1/
//--db2/
let longJohnBooks = await db.collection('books').find({
'author.firstName': 'John',
'pages': {
'$gte': 300
}
}).toArray();
- Understands nested queries like
{'author.firstName': 'John'}
- Accepts the following operators: $ne $gt $gte $lt $lte $in $nin $eq $exists
- Can query values in a document array.
{tags: 'blue'}
will select{_id: '', tags: ['blue', green]}
, but does not understand array query operators ($all, $elemMatch, $size)
await db.collection('book').insertOne({
title: 'Good Book',
author: 'John Doe',
reviews: 'positive'
})
await db.collection('book').findOneAndUpdate(
{title: 'Good Book'},
{
$rename: {title: 'lametitle'},
$set: {author: 'Jane Doe'},
$unset: {reviews: true}
}
);
/*{
lametitle: 'Good Book',
author: 'Jane Doe'
}*/
- Doesn't accept shorthand updates like {author: 'Jane Doe'}
- Accepts the following operators: $inc $min $max $mul $set $unset $rename $currentDate $addToSet $pop $push
let firstNames = await db.collection('book').find({}).project({
{'author.firstName': 1, _id: 0, title: 1}
}).toArray();
/*[
{author: {firtName: 'John'}, title: 'My Book'},
{author: {firstName: 'Jane'}, title: 'Hello World'}
]*/
- Accepts nested keys
- Doesn't accept operators
Returns the collection. If the collection doesn't exist, a new collection and .json file will be created
Sets the parent directory of the "mongfaux" folder where .json files are saved. Directory is process.cwd() by default.
Sets the database name. Also uses "mongfaux/dbname" folder for .json files. Has "name()" alias.
Does nothing
Autogenerates _id key and inserts document into the collection
- Returns e.g.
{insertedCount: 1, ops: [document], insertedId: id}
Autogenerated _id key for each document and inserts each document.
- Returns e.g.
{insertedCount: 1, ops: documents, insertedIds: []}
Finds one or more documents in the collection.
- Requires .toArray() to return a promise.
- Option object accepts: sort, skip, limit, and projection
Finds a single document.
- Does not require .toArray()
- Options object accepts: sort, skip, and projection options
Finds and updates a single document.
- Returns e.g.
{value: document, ok: 1}
. - Options object accepts: sort, upsert, returnOriginal, projection
- returnOriginal is
true
by default
Updates a single document without returning any documents.
- Returns e.g.
{matchedCount: 1, modifiedCount: 1, upsertedId: id}
- Options object accepts: upsert
Updates many documents without returning any documents.
- Returns e.g
{matchedCount: 1, modifiedCount: 1, upsertedId: id}
- Options object accepts: upsert
Finds, returns, and deletes a single document.
- Returns e.g.
{ok: 1, value: document}
- Options object accepts: sort, projection
Deletes a single document without returning any documents.
- Returns e.g.
{deletedCount: 1}
Delete multiple documents without returning any documents.
- Returns e.g.
{deletedCount: 1}
Finds a single document and replaces it.
- Returns e.g.
{value: }
- Options object accepts: sort, upsert, returnOriginal
- returnOriginal is
true
by default
Replaces a document without returning any documents.
- Returns e.g.
{matchedCount: 1, modifiedCount: 1, upsertedId: id}
- Options object accepts: upsert
Sets a field projection for the query result
Skips over a number of documents at the beginning of the query result
Specifies the maximum number of documents to return
Specifies the exclusive upper bound for a specific index
- e.g
{countingid: 10000}
Specifies the inclusive lower bound for a specific index
- e.g
{countingid: 10}
- e.g
{date: -1}
for sorting by date in a descending order
- Finishes query and returns the result.
- Finishes query and returns the number of documents.