• Stars
    star
    240
  • Rank 168,229 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Let's pretend we have a real MongoDB

mongo-mock     Build Status

This is an in-memory 'pretend' mongodb. The goal is to make the interface compatible with the real mongodb module so they are interchangeable.

There are a TON of features for mongo and I can't write them all myself- so pull requests are encouraged! My initial goal was to provide basic CRUD operations to enable this to work as a throw-something-together tool.

Why?

Maybe you don't want to (or can't) connect to a MongoDB instance for your tests?
Maybe you want to throw together a quick example app?

Demo code

var mongodb = require('mongo-mock');
mongodb.max_delay = 0;//you can choose to NOT pretend to be async (default is 400ms)
var MongoClient = mongodb.MongoClient;
MongoClient.persist="mongo.js";//persist the data to disk

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, {}, function(err, client) {
  var db = client.db();
  // Get the documents collection
  var collection = db.collection('documents');
  // Insert some documents
  var docs = [ {a : 1}, {a : 2}, {a : 3}];
  collection.insertMany(docs, function(err, result) {
    console.log('inserted',result);

    collection.updateOne({ a : 2 }, { $set: { b : 1 } }, function(err, result) {
      console.log('updated',result);

      collection.findOne({a:2}, {b:1}, function(err, doc) {
        console.log('foundOne', doc);

        collection.removeOne({ a : 3 }, function(err, result) {
          console.log('removed',result);

          collection.find({}, {_id:-1}).toArray(function(err, docs) {
            console.log('found',docs);
            
            function cleanup(){            
              var state = collection.toJSON();
              // Do whatever you want. It's just an Array of Objects.
              state.documents.push({a : 2});
              
              // truncate
              state.documents.length = 0;
              
              // closing connection
              db.close();
            }
            
            setTimeout(cleanup, 1000);
          });
        });
      });
    });
  });
});

Install

Well, you know.. the usual:

$ npm install mongo-mock

More Repositories

1

ipp

Internet Printing Protocol (IPP) for nodejs
JavaScript
412
star
2

litesocket

Realtime server events for Node.JS, using Server Sent Events (SSE)
JavaScript
30
star
3

webserial

A browser based serial app
Vue
28
star
4

sqlbits

An assortment of powerful & well tested functions that assist in building SQL statements
JavaScript
16
star
5

irc-replies

RFC 1459 IRC reply codes
14
star
6

nodeirc

Information about the Node.js IRC community
HTML
14
star
7

irc-connect

Minimal IRC connection that simply emits event objects. Plugin modules can do the rest.
JavaScript
14
star
8

scomm

A cross browser window/tab messaging framework.
JavaScript
8
star
9

extensions

Doin' freaky nasty things to the built-in prototypes.
JavaScript
8
star
10

propex

Describe objects with Property Expressions
HTML
8
star
11

node-eachline

Streams2 line reader
JavaScript
6
star
12

orgbot

Keep your GitHub owners to a minimum- use this bot to manage your org instead.
JavaScript
6
star
13

amazon-drive

A Node.js client for Amazon Drive
JavaScript
6
star
14

vl53l0x

A Node.js library for a vl53l0x device
JavaScript
6
star
15

ads1115

A Node.js library for an ads1115 analog to digital converter
JavaScript
5
star
16

allow

define and validate objects using a propex
JavaScript
5
star
17

irc-channels

Add channel features to irc-connect
JavaScript
5
star
18

ssd1327

A Node.js library for an ssd1327 oled display
JavaScript
4
star
19

comm

Decoupled javascript communication library
JavaScript
3
star
20

ezfs

Create/Read/Delete files and directories like they are JavaScript objects!
JavaScript
3
star
21

node-eachfile

Iterate over the files of a directory
JavaScript
3
star
22

bndlr

Static Content Bundler
JavaScript
3
star
23

consumer

Consume Javascipt strings character-by-character.
JavaScript
3
star
24

propex-validation

Uses Propex to validate javascript objects
JavaScript
3
star
25

crap

Ⓒontrollers Ⓡesources Ⓐnd ⓟroviders framework
JavaScript
2
star
26

i2cdriver

A Node.js library for a i2cdriver device http://i2cdriver.com
JavaScript
2
star
27

hook-relay

A server that accepts POSTs from Github Webhooks and relays them to Server Sent Event clients
JavaScript
2
star
28

sixchars

Encode/Decode random looking 6 character strings for a number.
JavaScript
2
star
29

AAAA

Some things I find useful
C#
2
star
30

get-then

Give a URL, Get a Buffer.
JavaScript
2
star
31

ldml

Read CLDR XML data with inheritance
JavaScript
1
star
32

tfmini-plus

A Node.js library for the TFMini Plus
JavaScript
1
star
33

amazon-drive-sync

A Node.js Amazon Drive synchronization service
1
star
34

vl53l1x

A Node.js library for a vl53l1x device
JavaScript
1
star
35

effd

Æ’ them Promise()s!!
JavaScript
1
star
36

bndlr-advanced

An example of using bndlr with more advanced requirements
JavaScript
1
star
37

bndlr-basic

An example of using bndlr with a simplistic website structure.
JavaScript
1
star