Socket
Socket
Sign inDemoInstall

mongoose-repl

Package Overview
Dependencies
13
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-repl

A Mongo REPL with the full power of Mongoose


Version published
Weekly downloads
1
decreased by-80%
Maintainers
1
Install size
2.57 MB
Created
Weekly downloads
 

Readme

Source

mongoose-repl

A Mongo REPL with the full power of Mongoose.

If you're using the Mongoose ODM to keep your MongoDB code from descending into schema-less chaos, it can be a pain to fall back to the barebones Mongo shell when you want to interact with your data.

mongoose-repl registers your schemas and exposes the resulting models in an interactive REPL, so you can findById and populate to your heart's content. You'll never have to type ObjectId again! Oh, and did I mention it interprets CoffeeScript?

$ mongoose --schemas examples/schemas.coffee localhost/test
> Dog.findById('520bb7baa4e06dc4e9000001').select('enemy name').populate('enemy')
{ name: 'fido',
  enemy: { name: 'fluffy', _id: 520baec9d61fae3ee6000001, __v: 0 },
  _id: 520bb7baa4e06dc4e9000001 }
> Cat.findById _.enemy._id
{ name: 'fluffy', _id: 520baec9d61fae3ee6000001, __v: 0 }

Installation

npm install -g mongoose-repl

Usage

Connecting to a DB

To connect to a Mongo instance, simply pass in a MongoDB connection string:

$ mongoose localhost/test 
Connecting to: localhost
Using db: test
No models loaded
>

Now you have access to the Mongoose connection object:

> conn.host
'localhost'
> conn.name
'test'
> conn.db
{ domain: null,
  _events: 
   { close: [Function],
     error: [Function],
 ...

Loading Models

Notice that no models were loaded, so the REPL isn't very useful yet. In order for mongoose-repl to give you access to Mongoose models, you have to first tell it about your schemas. Create a module that exports a mapping from schema names to schemas, like so:

$ cat examples/schemas.coffee
{Schema} = require 'mongoose'
module.exports =
  Cat: new Schema
    name: String
  Dog: new Schema
    name: String
    enemy: { type: Schema.Types.ObjectId, ref: 'Cat' }

Now you can load these schemas into the REPL using the --schemas (-s) option, which will register them as Mongoose models:

$ mongoose --schemas examples/schemas.coffee localhost/test
Connecting to: localhost
Using db: test
Loaded models: Cat, Dog
> Cat.modelName
'Cat'

Querying

This is where things get awesome. Now you can query models just like you do in Mongoose:

> Dog.findOne().populate('enemy')
{ name: 'fido',
  enemy: { name: 'fluffy', _id: 520baec9d61fae3ee6000001, __v: 0 },
  _id: 520bb7baa4e06dc4e9000001,
  __v: 0 }

The special variable _ holds the value of the last expression, so you can use it in further queries:

> Cat.find name: _.enemy.name
[ { name: 'fluffy', _id: 520baec9d61fae3ee6000001, __v: 0 } ]

FAQs

Last updated on 20 Aug 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc