Skip to content
Phil Myers edited this page Mar 5, 2016 · 15 revisions

Frequently Asked Questions (FAQs) - Course 3 Module 1

If you have have a general question that is not specific to the Module 1 content, it may have already been answered here.

Table of Contents

Q: The data directory not found or permission denied

A: Create the data directory C:\data\db (windows) or /data/db (linux)

In windows:

C:\>mkdir -p C:\data\db
``` C

In Linx or Mac, as `root` user:

$ mkdir -p /data/db $ chown -R mongod:mongod /data/db

Otherwise:

$ sudo mkdir -p /data/db $ sudo chown -R mongod:mongod /data/db


See also:
* [mongodb Mongod complains that there is no /data/db folder](http://stackoverflow.com/questions/7948789/mongodb-mongod-complains-that-there-is-no-data-db-folder)
* [MongoDB Setup - Data Directory Not Found or Permission Denied](http://wesleytsai.io/2015/07/26/mongodb-server-directory-permission-denied/)

[Back To Top](#table-of-contents)

-

### Q: WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

After installing and running the mongo server,

$ mongod


Then in a another terminal, when running the mongo client,

$ mongo



The following warnings are shown:

Server has startup warnings:

2016-01-19T20:59:55.320-0800 I CONTROL [initandlisten]

2016-01-19T20:59:55.320-0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000


What to do?

**A:** In Linux, set the resource limit just before starting `mongod` with this:

```shell
$ ulimit -n 1024
$ mongod

Back To Top

Q: mongod waiting for connections on port 27017

After doing mongod it shows:

[initandlisten] waiting for connections on port 27017

In the video a connection was accepted, that line is missing on my computer. Is this a problem?

A: No, this is not a problem. That was probably because a client running was already running in the video.

When you start clean, mongod should just come and wait for connections. This is the MongoDB database server.

When you start mongo then you should see the connection accepted message since this is the client connecting to the database server.

Back To Top

Q: Error connecting to db server: no reachable servers

This error shows up when try to run mongoimport:

mongoimport --db test --collection zips --drop --file zips.json

A: This happens if MongoDB Server (mongod) is not running. In a different terminal, go ahead and start mongod and then do an import.

Back To Top

Q: NameError: uninitialized constant Mongo::Client

In irb,

rb(main):001:0> require 'mongo'
=> true
irb(main):002:0> db = Mongo::Client.new('mongodb://localhost:27017')
NameError: uninitialized constant Mongo::Client
    from (irb):2
    ...

A: This error is because the `mongo`` ruby driver was not loaded properly.

First in the command shell ensure that you have installed the ruby gems by running:

C:\>gem install mongo
C:\>gem install bson_ext

Now start irb and run the command:

C:\>irb

irb(main):001:0> require 'mongo'
=> true

irb(main):004:0> db = Mongo::Client.new('mongodb://localhost:27017')
D, [2016-01-23T09:33:16.963650 #4156] DEBUG -- : MONGODB | Adding localhost:27017 to the cluster.
=> #<Mongo::Client:0x24416640 cluster=localhost:27017>

Back To Top

Q: Hash Rocket Notation vs. JSON Notation

Is there any particular reason for choosing to use the :symbol => "value"`` (Hash Rocket) notation or symbol: "value"` (JSON) notation?

A: The GitHub Ruby style guide suggests just to use the hash-rocket syntax for Hash literals and Rails style guide prefers JSON.

So it really comes down to your preference and in which side of the fence you are on.

See also:

Q: Submission is failing because of directory structure

I'm seeing the error:

Application not found in root directory of archive. This may be an invalid archive format or application scoped below an extra directory.

How can I fix this?

A: You need to fix the directory structure of your submission. The Gemfile, the app directory, etc. need to be in the root of your zip, not in a folder. Let's say your app, Gemfile, etc. are in the my_project directory. You can ensure your zip is structured correctly, you can run the following commands:

cd my_project
zip -r my_project.zip .

In plain English, the zip command means zip the contents of my current directory (i.e. the .) recursively (the -r) and name it my_project.zip.

Q: Unexpected nil error

I'm seeing a failing unit test where a value is expected to be not nil, but it is nil. How can I fix this?

A: One of the things you can check is whether there's a mismatch with your hash keys.

When implementing a method that takes a hash, be sure to account for the inputs being either string keys or symbol keys.

  • use to_s when you know you are going to compare to a String
  • use to_sym when you know you are going to compare to a symbol
  • quit - terminate the ruby process

Within Rails you can also:

  • use stringify_keys, stringify_keys!, deep_stringify_keys, and deep_stringify_keys! to convert all keys of a Hash to Strings to assure you that you are working with Strings.
  • use symbolize_keys and symbolize_keys!, deep_symbolize_keys and deep_symbolize_keys! to convert all keys of a Hash to Symbols to assure you are working with Symbols.

This specifically was encountered during the scaffold_spec.rb tests for Course #3, Module 1, summative assignment when the Racer.initalize() method was written to process String keys and not Symbol keys for the input has. This resulted in capybara inserting an empty document into the database and the test unable to locate the document using a first_name and last_name search.

Q: Issue with Mongoid Database Connection

I'm having issues with step #3 - configuring the Mongoid Database connection.

A: Don't copy directory from the directions. The directions use backticks (`) instead of single quotes ('). Make sure you use

Mongoid.load!('./config/mongoid.yml')

not

Mongoid.load!(’./config/mongoid.yml’)

Q: How to Debug Rspec Test

How can I get more information about my failing rspec test?

A: Use the debugger. First add the byebug debugger to your Gemfile:

#Gemfile
group :development, :test do 
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
  gem 'byebug' 
end

Next, add byebug to your code where you wish to set a breakpoint:

#spec/crud_spec.rb
context "rq06" do
  before :each do
    byebug  #<<<<<< add this
    db_rec = Racer.collection.find(number: 300).first
    @id = db_rec[:_id].to_s
    @racer = Racer.find(@id)
  end
end

Execute your test and the terminal should show a list of your code and a byebug prompt. You can find documentation on using byebug here. Here is a video demonstration of using byebug. As a quick reference, some of the most useful commands are:

  • list - show where I am if screen gets cluttered
  • var - list of variables (local, instance, etc.)
  • (variable name) - state of a specific variable
  • next - step over function
  • step - step into function
  • (any valid Ruby command) - to do whatever you want like query the database
  • continue - to go to the next breakpoint or execute to end if no more breakpoints

In the more interactive example below from crud_spec.rb, when we go to call Racer.find, we see that @id is a string. We can call Racer.find() ourselves to see if a result comes back or step into the call to see what went wrong.

156:             @id = db_rec[:_id].to_s=> 
==>157:          @racer = Racer.find(@id)   
158:         end   
159:    
(byebug) @id"56a677d9e301d00d3f00012c"
(byebug) Racer.find(@id)#<Racer:0x007fa98400f110 @id="56a677d9e301d00d3f00012c", ... 
(byebug) Racer.collection.find(:_id=>BSON::ObjectId.from_string(@id)).first
{"_id"=>BSON::ObjectId('56a677d9e301d00d3f00012c'), "number"=>300, 
(byebug) continue

Q: Why are tests passing locally but not remotely?

My tests are passing locally but not remotely. How can I debug this?

A: One thing you can check for is whether you're using correct Ruby naming conventions for your file names and class names. Class names should be "CamelCase" and file names should be "snake_case".

For example, if you're getting the following error:

Racer implements a class method called mongo_client (FAILED - 1)

Racer implements a class method called collection to access collection racers (FAILED - 2)

Check the case of your class filenames:

$ tree app/models/app/models/
|-- concerns
`-- Racer.rb

FYI - tree is a unix utility used to visualize the directory structure that is not included on OS X by default. To install it, run brew install tree. If you're running Windows, you're on your own ;)

Filenames must be in snake_case when submitted to the grader and to be compliant with Rails file naming conventions.

$ tree app/models/app/models/
|-- concerns
`-- racer.rb 

Here is a link to the Ruby style guide for reference.

Q: How to query an array?

Examples in the video are about doing operations on single values of let's say, strings, or numbers. How do we go about querying an array field?

For example, how can we apply $lt and $gt to match against the ":loc" field present in the example data set?

A:

> Zip.collection.find.first
=> {"_id"=>"99743", "city"=>"HEALY", "loc"=>[-149.011128, 63.917123], "pop"=>1058, "state"=>"AK"}

We can use the name of the array to match any value within the array

> Zip.collection.find(:loc=>-149.011128).first
=> {"_id"=>"99743", "city"=>"HEALY", "loc"=>[-149.011128, 63.917123], "pop"=>1058, "state"=>"AK"}

> Zip.collection.find(:"loc"=>63.917123).first
=> {"_id"=>"99743", "city"=>"HEALY", "loc"=>[-149.011128, 63.917123], "pop"=>1058, "state"=>"AK"}

> Zip.collection.find(:"loc"=>111).first
=> nil

That also means that we can use comparison operators as well and assume or the time being that our array represents a list of values of common type (e.g. zip codes) and not different fields as it currently does.

> Zip.collection.find(:"loc"=>{:$gt=>-150, :$lt=>-149}).first => {"_id"=>"99743", "city"=>"HEALY", "loc"=>[-149.011128, 63.917123], "pop"=>1058, "state"=>"AK"}

If you wish to test specific elements of the array, you can add an array index to the key name.

> Zip.collection.find(:"loc.0"=>{:$gt=>-150, :$lt=>-149}).first
=> {"_id"=>"99743", "city"=>"HEALY", "loc"=>[-149.011128, 63.917123], "pop"=>1058, "state"=>"AK"}
> Zip.collection.find(:"loc.1"=>{:$gt=>-150, :$lt=>-149}).first
=> nil

Back To Top

Clone this wiki locally