Using mongo via terminal for DB manipulation


$ mongo

MongoDB shell version: 3.0.4
connecting to: test

> show dbs

bookAPI 0.078GB
local 0.078GB
my_database_name 0.078GB

CREATE A DATABASE

Don’t worry about “connecting to” … that’s just the default database Mongo decides to use if you don’t specify one on the command line, which we didn’t because it’s not important right now. It doesn’t actually even create the “test” database unless you add a record. It’d be totally fine to just work in that database for right now, but let’s make one of our own. In your Mongo console, type the following:

Now we’re using the database “test1.” Nothing actually exists yet. To make the database exist, we have to add some data. We’re going to start off by doing that right inside of the Mongo client.

ADD SOME DATA

MongoDB uses JSON for its structure.

Let’s add a record to our collection. Note that we are currently ‘on’ database test1. So whatever we insert will be into database ‘test1’. Our data format will thus look like this:

Something important to note here: that “db” stands for our database, which as mentioned above we’ve defined as “test1”. The “usercollection” is the name of our collection in this database. Its kind of like ‘table’ in a SQL database. Every JSON we insert is like a row. Note that there wasn’t a step where we created the “usercollection” collection. That’s because the first time we add to it, it’s going to be auto-created. OK, Hit enter. Assuming everything went right, you should see … nothing. That’s not very exciting, so type this:

Showing all data from table

You access the table name by saying ‘db’ for current database, then db.tableName, which in our case is users, so it would be db.users. Finally, we use find() to display all data so in the end it would be:

db.users.find()

output:

{ “_id” : ObjectId(“559261a6250879f91747a6a5”), “name” : “MODULUS ADMIN”, “age” : 42, “roles” : [ “admin”, “moderator”, “user” ], “__v” : 0 }
>

Find where age > “—“


> db.users.find( { age: { $gt: 25 } } )

{ “_id” : ObjectId(“559261a6250879f91747a6a5”), “name” : “MODULUS ADMIN”, “age” : 42, “roles” : [ “admin”, “moderator”, “user” ], “__v” : 0 }

> db.users.find( { age: { $gt: 43 } } )

>

Clear out a Collection

Removing objects where name is “—“


> db.users.remove({name:”MODULUS ADMIN”})

WriteResult({ “nRemoved” : 1 })

> db.users.find()

>

counting how many are in a table


> db.users.count()

0

Insert

If the collection does not exist, the insert() method creates the collection.

> db.users.insert( { name: “ricky”, age: 35, roles:”admin” } )

Dropping Collections


db.{collection name}.drop()

Dropping Database


use my_database_name

switched to db my_database_name

> db.dropDatabase()

{ “dropped” : “my_database_name”, “ok” : 1 }

Viewing contents of a database

first show all database

> show dbs;

PersonDatabase 0.078GB
local 0.078GB

Then pick a database to use

> use PersonDatabase;

switched to db PersonDatabase

Then show all collections inside that database

> show collections

people
system.indexes

Then do db.{collection name}.find()

> db.people.find()

{ “_id” : ObjectId(“55939a0800f3e7343974a41f”), “name” : “Ricky-dude”, “username” : “rtsao”, “password” : “compaq”, “admin” : true, “__v” : 0 }
{ “_id” : ObjectId(“55939adc2b0f71473b38b3d6”), “name” : “Ricky-dude”, “username” : “hadoken”, “password” : “compaq”, “admin” : true, “__v” : 0 }
{ “_id” : ObjectId(“55939b81648f93fd3bb1f4f1”), “name” : “Ricky-dude”, “username” : “shoryuken”, “password” : “compaq”, “admin” : true, “__v” : 0 }

inserting dates via terminal

db.badmintonEvents.insert({ “attendee_name” : “badminton_2-18-2016”, “date” : ISODate(“2016-02-18T00:00:00Z”), “description” : “8:00pm at baishizhou” })