3

The problem

I have successfully launched the mongo shell on my MAC OSX environment. I am resourcing the following links and documentation to figure out the simple task of getting into a database, then a collection, then querying documents within the collection: Accessing a database from MongoDB documentation, Accessing a collection and documents, here on Stackoverflow, and Accessing a collection from Tutorials Point.

I created and loaded the database through the PyMongo API. This successfully created a database named UCI-Database, a collection named income, and filled it with a bunch of documents (rows) from .csv document.

So far ..

These are my results...

Blakes-MacBook-Pro:nosql bmc$ mongo 127.0.0.1:27017
MongoDB shell version v3.4.4
connecting to: 127.0.0.1:27017
MongoDB server version: 3.4.4
Server has startup warnings: 
2017-05-16T12:22:10.147-0400 I CONTROL  [initandlisten] 
2017-05-16T12:22:10.147-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-05-16T12:22:10.147-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-05-16T12:22:10.147-0400 I CONTROL  [initandlisten] 
2017-05-16T12:22:10.147-0400 I CONTROL  [initandlisten] 
2017-05-16T12:22:10.147-0400 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs
UCI-Database   0.006GB
admin          0.000GB
local          0.000GB
test-database  0.000GB
> use UCI-Database
switched to db UCI-Database
> show collections
income
profiles
> 

And income is for sure the collection I created to store my rows from UCI's Adult income repository. As stated in the mongo db documentation:

db.collection.find(query, projection)

Is the syntax to query a collection.

My best attempt

> db.UCI-Database.income.find({"age":35})
2017-05-16T16:38:00.846-0400 E QUERY    [thread1] ReferenceError: Database is not defined :
@(shell):1:1
> 

Any feedback is helpful.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
bmc
  • 817
  • 1
  • 12
  • 23

1 Answers1

10

The problem is that you are not doing

db.collection.find(query, projection)

To query the collection you want remove the database name from the command, i.e.

use UCI-Database
db.income.find({"age":35})
Daniel Squires
  • 338
  • 3
  • 13
  • It worked. This was a very helpful comment. Do you have any good resources for learning to query and become more comfortable with mongodb? Things like `count` and `distinct` like in SQL? – bmc May 16 '17 at 21:08
  • 1
    @bmc these classes offered by https://university.mongodb.com/ are pretty great. I recommend M101 course to get up and running with mongodb – Samip Suwal May 17 '17 at 02:34