The MongoJS Cheat Sheet

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re looking to use a MongoDB database with your web application running on NodeJS you might be looking at which npm libraries which will allow you to easily interact with MongoDB. MongoJS and Mongoose are the two top choices for developers in this scenario. In this article we’ll give a quick cheat sheet of the common commands you’ll use with MongoJS that will quickly get you oriented MongoJS. We’ll show basic commands and a quick briefing on what each command does.

  • Note: To demo we will use a grocery store db grocerydb and a products collection containing grocery products.

Cheat Sheet

Creating

Create a database or switch to a database. ( You should know that the database is not actually created until you create a collection within it )

1
use grocerydb

Display the database you are currently working with

1
db

Create a collection and insert a document in it

1
db.products.insert({name: "Soda", quantity: 300, brand: "DemoBrand", departments: ["Checkout", "Beverages"]})

Finding

Get all the documents in a collection

1
db.products.find()

Use the .pretty() to make finds more readable

1
db.products.find().pretty()

Find documents with a matching field

1
db.products.find({name: "Soda"})

Find documents matching multiple fields

1
db.products.find({name: "Soda", brand: "DemoBrand"})

Find documents with a matching entry in an array field

1
db.products.find({departments: {$in: ["Beverages"]}})

Find a document by _id

1
db.products.find({_id: ObjectId("defe16fe1d94bcf86cd78543defe")})

Updating

Update a document. This example will update the soda entry brand. This will only update the first entry where name = “Soda”. If you want to update multiple documents we will show you how to do that next.

1
db.products.update({"name": "Soda"}, {$set: {"brand": "newBrand"}})

Update multiple documents

1
db.products.update({"name": "Soda"}, {$set: {"brand": "newBrand"}}, {multi: true})

Add a value to an array field

1
db.products.update({"name": "Soda"}, {$push: {"departments": "Clearnace"}})

Deletion

Delete a document

1
db.products.remove({"name": "Soda"})

Delete all documents in a collection

1
db.products.remove({})

Drop/Delete a collection

1
db.products.drop()

Drop/Delete a database

1
db.dropDatabase()

Conclusion

In this cheat sheet we gave you the basic CRUD ( Create, Read, Update, Delete ) commands that will be used with MongoJS. MongoJS is a very easy library to get up and running with so get started and try out some commands. You can get MongoJS from npm by running npm install mongojs. There are tons of variations and more commands out there for MongoDB but this should serve as a good starting point and reference for anybody using the MongoJS npm library. If you need more help defining a schema for your data, then you might look into the Mongoose library which is great for that scenario. MongoJS provides functions for all MongoDB functionality but you’ll have to manage the schema yourself. It’s more of a low-level library than Mongoose which has it’s advantages and disadvantages depending on your application.

We hope you find this cheat sheet helpful in getting acquainted with MongoJS. If you have any feedback on commands we could add please don’t hesitate to reach out to us.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.