Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MongoDB Shell Cheat Sheet, Lecture notes of Advanced Computer Programming

Starting the mongo shell. Mongo shell can be used to connect to local databases, or remote databases running on another server. If connecting to MongoDB ...

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

journalyyy
journalyyy 🇬🇧

4.7

(12)

215 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MongoDB Shell
Cheat Sheet
mongo Connect to a local MongoDB database
mongo <hostname>:<port>
<databaseName> -u <user> -p
<password>
e.g.:
mongo cianclarke.com:27017/users
-u cianclarke -p mYs3cretz
Protip: The default port MongoDB runs on
is 27017.
Connect to a remote MongoDB server
show dbs Shows all databases available on
this server
use acmegrocery Switches to a database called acmegro-
cery. Creates this database if it doesn’t
already exist.
show collections Show all collections in the current db (first
`use <someDb>`)
show users Show all users for the current DB
show roles Show the roles defined for the current DB
db.apples.find() Finds all documents in a collection
named “apples”.
Starting the mongo shell
Mongo shell can be used to connect to local databases, or remote databases running on
another server. If connecting to MongoDB locally, make sure its running in another terminal
window or background process (the `mongod` command).
Navigating around Mongo
Here are the most used commands, operations and queries.
Working with a collection
Now that you have selected a database and listed the collections inside, you can perform
operations using the `db` variable.
Every collection has a property on the `db` variable - e.g. the `apples`
collection is `db.apples`.
Code
Help Methods and Commands
JavaScript Database Operations
Description
Description
Description
pf3

Partial preview of the text

Download MongoDB Shell Cheat Sheet and more Lecture notes Advanced Computer Programming in PDF only on Docsity!

MongoDB Shell

Cheat Sheet

mongo (^) Connect to a local MongoDB database

mongo : -u -p e.g.: mongo cianclarke.com:27017/users -u cianclarke -p mYs3cretz Protip: The default port MongoDB runs on is 27017.

Connect to a remote MongoDB server

show dbs (^) Shows all databases available on this server

use acmegrocery (^) Switches to a database called acmegro- cery. Creates this database if it doesn’t already exist.

show collections (^) Show all collections in the current db (first use <someDb>)

show users (^) Show all users for the current DB

show roles (^) Show the roles defined for the current DB

db.apples.find() (^) Finds all documents in a collection named “apples”.

Starting the mongo shell

Mongo shell can be used to connect to local databases, or remote databases running on another server. If connecting to MongoDB locally, make sure it’s running in another terminal window or background process (the **mongod** command).

Navigating around Mongo

Here are the most used commands, operations and queries.

Working with a collection

Now that you have selected a database and listed the collections inside, you can perform operations using the **db** variable. Every collection has a property on the **db** variable - e.g. the **apples** collection is **db.apples**.

Code

Help Methods and Commands

JavaScript Database Operations

Description

Description

Description

db[“blood-oranges”].find() (^) As above, finds all documents in a collec- tion named “seville-oranges”. Collection names with characters that are reserved in JavaScipt (in this case, your - character) need to be queried this way.

db.apples.find({type : “granny smith”})

Finds all documents in a collection called “apples” with type matching “granny smith”.

db.apples.find({}, { type : 1, price : 1 })

You can also find all apple documents, but only return the fields we’re interested in - in this case, type and price, by setting them to 1 in the second parameter.

db.apples.find().sort({ price : -1 })

You can sort the results from a find() op- eration using the sort function. -1 sorts descending, 1 sorts ascending.

JavaScript Database Operations (^) Description

db.apples.remove({ bad : true}) (^) Removes all bad apples! Finds all apples with a property of “bad” set to true, and removes them.

db.apples.update({ type : “granny smith”}, {$set : { price : 2. }})

#don’t do this! db.apples.update({ type : “granny smith”}, { price : 2.99 })

Updates the price of all granny smith apples.

Important! Note the $set syntax - this

doesn’t work as many would expect. With- out putting the updated fields inside a $set clause, we replace the entire document.

db.apples.insert({ type : “granny smith”, “price” : 5.99 })

Inserts a new document into the apples collection. If your apples collection doesn’t exist, it will get created.

db.apples.findOne({ _id : ObjectId (“54324a5925859afb491a0000”) })

Looking up a document by ID is special. We can’t just specify the ObjectID as a string - we need to cast it to an ObjectId.

Changing Groups of Documents

All of the operations so far have been queries which return many documents - called a cursor. You could use these cursors and iterate, but there’s an easier way.

Working with Individual Documents

All of these operations do something with many documents, but in the Mongo Shell there’s a really neat way of working with just one document too!

If you haven’t already noticed, the mongo shell is a JavaScript REPL. This means you can use basic JavaScript commands to operate on documents!