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
66
votes
3 answers

monk vs mongoose for Mongodb

I'm learning NodeJs. To connect to and use MongoDB from NodeJS, I see a lot of examples using either Monk or Mongoose. Are these two libraries equivalent ? Do they have the same features or do they each have a specific purpose ? As a beginner with…
Young
  • 969
  • 1
  • 8
  • 18
43
votes
15 answers

Argument passed in must be a string of 24 hex characters - I think it is

I have a method to find a document in my database based on its ObjectID: console.log('id: ' + id + ' type: ' + typeof id); collection.findOne({'_id':new ObjectID(id)}, function(error,doc) { if (error) { …
Carasel
  • 2,740
  • 5
  • 32
  • 51
9
votes
1 answer

how to get data in batches in mongodb

I want to retrieve data from MongoDB, 5 at a time I am using limit to limit the number of records returned router.post('/List', function (req, res) { var db = req.db; var collection = db.get('clnName'); collection.find({}, { limit: 5 *…
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
8
votes
3 answers

MongoDB database deleted automatically

I am very confused with the MongoDB behavior I am facing these days. I am running a MEAN(MongoDB, Express, Angular, NodeJS) application on a windows live server. Two times it happened that the whole database my application is connected with is…
HMUS
  • 173
  • 1
  • 1
  • 7
8
votes
2 answers

create a new object Id in mongoDB using node js

I am using the below code to insert data to mongodb router.post('/NewStory', function (req, res) { var currentObject = { user: userId , story : story , _id:new ObjectID().toHexString() }; req.db.get('clnTemple').findAndModify({ …
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
6
votes
1 answer

Using the find method on a MongoDB collection with Monk

I am working through a MEAN stack tutorial. It contains the following code as a route in index.js. The name of my Mongo collection is brandcollection. /* GET Brand Complaints page. */ router.get('/brands', function(req, res) { var…
jdw
  • 3,755
  • 3
  • 17
  • 16
5
votes
1 answer

Multiple filters in mongoDB

Theres is a part of my application where i can specify some filters to retrieve certain data. But not all the filters are necessary so i can left some of them empty and the query just wouldn't use them. How can i achieve this in mongoDB. Thanks in…
DCruz22
  • 806
  • 1
  • 9
  • 18
4
votes
5 answers

DB connection error handling with monk

I am using monk on a code that looks like var monk = require('monk') var db = monk('localhost/mydb') if(!db){ console.log('no connection') } when I run it, console logs 'no connection', but I would like to know why it is not connecting, (maybe…
Alloys
  • 143
  • 1
  • 11
4
votes
0 answers

Forcing Mongodb errors for unit testing in NodeJS

I'm writing my first tests for a Mongodb database using should.js, and need a way to force errors in various functions that make database calls. I've spent a little time exploring how Rewire.js might help, but am still new enough to Mongo that I'm…
4
votes
1 answer

MongoDB: map relationships in collection

I don't know how descriptive the title is, so feel free to modify it for better understanding. I'm very new to MongoDB and so far things have gone well. However I ran into a very basic problem where I need to join / map documents from other…
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
3
votes
1 answer

Mongo db with Monk: error catching and handling if db is down

I'm new to Mongo. I needed a database for a simple project and ended up following a tutorial using Mongo with Monk but I have problems understanding how to handle errors. Background: I have a registration form on the client side. When the user…
Tommy
  • 628
  • 11
  • 22
3
votes
1 answer

How to insert a document with a subdocument array element in MongoDB with Monk

when i want to insert a document in my mongodb with monk wich has an array element of subdocuments, the funcion insert it wrong in the db. I'm calling the insert function this way: var OrderItem = []; OrderItem[0] = { 'model': asd1, …
Nico
  • 51
  • 3
3
votes
1 answer

List Collection Names in MongoDB using Monk

In my Node.js (Express) app that uses Monk, I need to render a list of all collections in the MongoDB database. Is there a way to get the list of collections in the database using Monk?
Marc Bacvanski
  • 1,458
  • 4
  • 17
  • 33
3
votes
1 answer

How to limit connection pool size in monk Node.js

I am using monk module to get data from mongodb. But as soon as I connect to db it creates 5 connections. This is my command line mongodb server console... 2015-05-14T10:32:35.618+0530 I NETWORK [initandlisten] connection accepted from …
Harish Mohanani
  • 77
  • 1
  • 13
3
votes
1 answer

Is there a way to call 'distinct' through monk?

I want to execute the following 'query' on my node.js server: MongoDB: db.example.distinct('field') Expected result: [value1, value2, ..., etc] I tried this Javascript, but it doesn't work: db.get('example').distinct('field', function (err,…
GoldfishGrenade
  • 621
  • 1
  • 6
  • 14
1
2 3
12 13