MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

MongoDB on Fedora 27

without comments

I was very pleased to see MongoDB 3.4 in the repo for Fedora 27; however, I was quite frankly severely disappointed by the lack of concrete installation steps in the MongoDB in Action, Second Edition. The installation is straightforward as long as you remember that you don’t simply install a mongodb package. You need to install the mongodb and mongodb-server packages as well as some dependents.

As the root user, you can use yum to install them like this:

yum install -y mongo*

After installing the correct packages, you can start the MongoDB Daemon as a service using the root account or a sudoer account (if you get warning messages, you need to create a mongod file):

service mongod start

Finally, you can connect to the MongoDB client, like so:

mongo

Inside the mongo shell, you can check the MongoDB version with the following command:

db.version()

It should print:

3.4.11

Or, you can check the statistics of a new empty instance:

db.stats()

A fresh installation of MongoDB 3.4.11 on Fedora 27 should return:

{
        "db" : "test",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}

You can insert data into MongoDB by creating a database, which is simply a file. You use an existing database or file by using the use command. If the database or file doesn’t exist already, the use command creates the database.

use videodb

You can create a collection or table with the insert function. The following creates an item collection and adds two JSON objects to the item collection.

db.item.insert({item:"Star Wars: A New Hope"})

It returns:

WriteResult({ "nInserted" : 1 })

You can create or add to an item collection or table with the same insert function call. You simply enclose the list of collection elements in the square brackets of a JavaScript array or list. The following inserts the second and third Star Wars films:

db.item.insert([{item:"Star Wars: The Empire Strikes Back"},{item:"Star Wars: Return of the Jedi"}])

It returns:

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]

You can query the collection with the find function, like:

db.item.find()

It returns:

{ "_id" : ObjectId("5b02732435b861850bc51ef1"), "item" : "Star Wars: A New Hope" }
{ "_id" : ObjectId("5b02734635b861850bc51ef2"), "item" : "Star Wars: The Empire Strikes Back" }
{ "_id" : ObjectId("5b02734635b861850bc51ef3"), "item" : "Star Wars: Return of the Jedi" }

You can write large JavaScript files for bulk inserts. For example, the following users.js file:

db.users.insert(
[
  { contact_account: "CA_20170321_0001"
  , first_name: "Jonathan"
  , middle_name: "Eoin"
  , last_name: "MacGregor"
  , addresses:
    {
      street_address: ["1111 Broadway","Suite 101"]
    , city: "Oakland"
    , state: "CA"
    , zip: "94607" 
    }
  }
, { contact_account: "CA_20170328_0001"
  , first_name: "Remington"
  , middle_name: "Alain" 
  , last_name: "Dennison"
  , addresses:
    {
      street_address: ["2222 Broadway","Suite 121"]
    , city: "Oakland"
    , state: "CA"
    , zip: "94607" 
    }
  }
])

You can call the file with the load command from the interactive MongoDB command line:

load("/home/student/Code/mongodb/users.js")

You can query the results after you load the collection elements in the users collection with the following command:

db.users.find()

It returns:

{ "_id" : ObjectId("5b0270d535b861850bc51eef"), "contact_account" : "CA_20170321_0001", "first_name" : "Jonathan", "middle_name" : "Eoin", "last_name" : "MacGregor", "addresses" : { "street_address" : [ "1111 Broadway", "Suite 101" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" } }
{ "_id" : ObjectId("5b0270d535b861850bc51ef0"), "contact_account" : "CA_20170328_0001", "first_name" : "Remington", "middle_name" : "Alain", "last_name" : "Dennison", "addresses" : { "street_address" : [ "2222 Broadway", "Suite 121" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" } }

In both cases, the item and user collections add a unique identifier. The unique identifier is much like a surrogate key in a relational database. That’s why you should always define a unique natural key in the definition of your collection.

As always, I hope this helps.

Written by maclochlainn

May 16th, 2018 at 6:05 pm