MongoDB – Introduction



MongoDB – Introduction

0 0


MongoDB-Introduction-Part-I

Introduction to MongoDB - Part I

On Github SUNY-Albany-CCI-LearningEncounters / MongoDB-Introduction-Part-I

MongoDB

Introduction

Created by Luis Ibanez

MongoDB Introduction by Luis Ibanez is licensed under a Creative Commons by Attribution 4.0 License Apache 2.0 License

MongoDB

Introduction

MongoDB

MongoDB

is a

Document Database

The

Data

Model

Starts

with a

"Database"

a "Database"

contains

"Collections"

a "Collection"

contains

"Documents"

a "Document"

contains

"Fields"

a "Field"

is a

"key-value" pair

MongoDB

uses

JSON

a "key-value" pair

uses the JSON notation

'key' : 'value'

Time to

Travel

Think of a Country and City

that you would like to visit

if someone else is paying for the trip.

From the Command Line

open the INF202World database:

mongo INF202World

See the available collections

show collections

One of the collections

is called

"cities"

See if your city exists

db.cities.find( { 'name' : 'New York' } )

but of course, use your city's names

If your city exists,

pick another one

that is not in the collection.

Insert your new city

db.cities.insert( { 'name' : 'Emerald City' } )

but of course, use your city's names

Verify that it worked

db.cities.find( { 'name' : 'Emerald City' } )

but of course, use your city's names

See all the city names

db.cities.find( {}, { 'name' : 1 } )

The "find"

function

has two arguments

The two arguments are:

db.cities.find( filter, question )

No need to type this in MongoDB

The "filter"

argument

narrows the search

The "question"

argument

selects the fields

we want to get back

When we do

db.cities.find( {}, { 'name' : 1 } )

the "filter" is empty

so we are getting all cities (document)

from the collection

Exit MongoDB shell

exit

Breath!

Smile!

open again the INF202World database:

mongo INF202World

See the available collections

show collections

See again your city's data

db.cities.find( { 'name' : 'New York' } )

but of course, use your city's names

Let's add more to it.

Update the document with the country.

db.cities.update( { 'name' : 'New York' }, { $set : { 'country' : 'USA' } } )

but of course, use your city's names

and it's respective country.

Note the '$set' operator.

db.cities.update( { 'name' : 'New York' }, { $set : { 'country' : 'USA' } } )

It is VERY important.

Without the '$set' operator

the new data will

REPLACE

the existing data.

Check again your city's data

db.cities.find( { 'name' : 'New York' } )

but of course, use your city's names

WARNING:

if at anytime you get the prompt

...

It means that you may not have closed all the {} or () or "".

The "update"

function

has two arguments

The two arguments are:

db.cities.find( filter, newdata )

No need to type this in MongoDB

The "filter"

argument

narrows the search

The "newdata"

argument

provides data to be added

or to replace current data

If "newdata"

uses

$set

then data will be added

If "newdata"

does not use

$set

then data will be replaced

Let's add food data.

db.cities.update( { 'name' : 'New York' },
  { $set : { 'food' : [
              'pizza',
              'sandwich'
              ]
           }
  }
  )

but of course, use your city's typical food

Check again your city's data

db.cities.find( { 'name' : 'New York' } )

but of course, use your city's names

Your

Turn !

Find all cities in a country

db.cities.find( { 'country' : 'USA' } )

Find all cities in a country

and get only their names

db.cities.find( { 'country' : 'USA' }, { 'name' : 1 } )

Find all cities in a country

and get only their names

without their _id

db.cities.find( { 'country' : 'USA' }, { 'name' : 1, '_id' : 0  } )

The same can be written

using multiple lines

db.cities.find(
    {
      'country' : 'USA'
    },
    {
      'name' : 1,
      '_id' : 0
     }
   )

Find all cities

in the same country

as your city

Every city

is a MongoDB

"Document"

The group of cities

is a MongoDB

"Collection"

Let's Add

More

Visit the

Wikipedia page

of your city.

Select information

to add to the

MongoDB Document.

Some pieces of data

will be

properties

in key:value format.

Some pieces of data

will be

in key:array format.

Breath!

Smile!