

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!
mongo (^) Connect to a local MongoDB database
mongo
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”.
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).
Here are the most used commands, operations and queries.
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.
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.
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.
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!