Questions tagged [monk]

Monk is a tiny layer that provides some usability improvements for MongoDB usage within Node.JS

Features (taken from monks github page):

  • Command buffering. You can start querying right away.
  • Promises built-in for all queries. Easy interoperability with modules.
  • Easy connections / configuration
  • Well-designed signatures
  • Improvements to the MongoDB APIs (eg: findAndModify supports the update signature style)
  • Auto-casting of _id in queries
  • Builds on top of mongoskin
  • Allows to set global options or collection-level options for queries. (eg: safe is true by default for all queries)

Connection

var db = require('monk')('localhost/dbname');

To set global multi-doc at db level

db.options.multi = true;

To set global multi-doc at collection level

db.get('users').options.multi = false;  

Close DB connection

db.close(); 

Operations

var collection = db.get('collection');    
collection.insert({ }, function(err) { });
collection.find({ }, function(err, docs) { });
collection.findOne({ }, function(err, doc) { });
collection.update({ }, { }, function(err, docs) { });
collection.findAndModify({ }, { }, function(err, docs) { });
collection.findById('id', function(err, doc) { });
collection.drop(function(err) { });

Index

collection.index('name.first', function() { });
Unique Index
collection.index('email', { unique: true });  
Compound Index
collection.index('name.first name.last')   
Compound with sort
collection.index({ 'email': 1, 'password': -1 }); 
To get indexes for a collection
collection.indexes(fn);
Drop an index
collection.dropIndex(name, fn);
Drop all indexes
collection.dropIndexes(fn);
184 questions
-1
votes
1 answer

What does ('localhost:27017/test') mean in the syntax var db = require('monk')('localhost:27017/test') in node.js?

I'm a complete novice in node.js and just have started working on it. I was following few tutorials online on 'mongoDB' and 'monk' and I came across this syntax var db = require('monk')('localhost:27017/test'). Now, I know we're requiring the 'monk'…
Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
-1
votes
2 answers

node.js express mongodb monk insert - error with no message

I have this code /* GET */ router.get('/mycol', function(req, res) { var db = req.db; var collection = db.get('mycol'); collection.find({}, {}, function(err, docs) { res.json(docs); }); }); /* POST */ router.post('/mycol', function(req,…
usrgnxc
  • 794
  • 2
  • 9
  • 34
-1
votes
2 answers

how to send a value to database through a separate module?

I have a basic express project created. And I've created a file called lib/userhandler.js inside root folder. //lib/userhandler.js exports.addUser = function(req, res){ // Set our internal DB variable var db = req.db; // Get our form…
user3280033
  • 11
  • 1
  • 5
-1
votes
3 answers

I am unable to push my Node.js with Mongodb backend project on Bluemix. Instance creation is failed

The following the log dump from my cf push : 2014-09-09T15:47:35.92+0530 [App/0] ERR 2014-09-09T15:47:35.97+0530 [DEA] OUT Instance (index 0) failed to start accepting connections 2014-09-09T15:47:55.71+0530 [DEA] OUT Starting app…
1 2 3
12
13