How to browse data in MongoDB in Mac OS? - ruby-on-rails

When I am using PostgreSQL, I am accustomed to use terminal for browsing data stored in DB tables.
Is there any similar way to do it for MongoDB? I have used this topic for MongoDB installation on Mac.
Thanks

MongoDB bin directory contains an executable called 'mongo' which is an interactive shell (similar to 'psql' in PostgreSQL)
You can read more about how to use it HERE.
To get started, you can type
> help
To switch to a specific database, just type:
> use db-name
^^^^^^^ replace with your db name.
> db.help()
> db.collectionName.help()
^^^^^^^^^^^^^^ replace with your collection name
You can do this from any machine not just the one mongod is running on but then you connect via:
mongo hostname:port/dbname
for example
mongo myMongoDBserver:27017/foobardb

First start mongod process in a terminal tab. In other terminal tab or window simply start mongo.
mongod is mongo daemon which establishes connections and listens to requests. mongo is the javascript shell where you can have your interactive mongodb queries.
Rest is best explained in the link #Asya Kamsky provided in his answer.

command 'mongo' will open mongo shell for you, there you can use database commands

Related

Can psql commands be executed from Rails application

I want to know whether psql commands be executed from an Application ?
For example I want to make use of \crosstabview functionality which psql provides. It is a great feature to have when viewing the reports.
I have an application which uses Ruby on Rails. I'm thinking whether I can run the \crosstabview from the application.
These are features of psql application only. There is no way to use them via database driver, be it ActiveRecord or anything else. That's just a different thing.
However, you can have table view using, for example, table_view gem.
Can psql commands be executed from Rails application?
It can theoretically be done by running psql from ruby. Its just a really clunky solution.
The psql --command option only takes a single function name or a SQL string that is parsable by the database and which cannot use psql specific functions. Which means that you can run for example %x{ psql --command "\\h"} but not \crosstableview which needs input.
That leaves using PTY to open an interactive session.
# example using PTY to connect to an interactive psql shell
require 'pty'
require 'expect'
PTY.spawn('psql') do |output, input, pid|
output.expect /\=\#/ do
input.puts '\\conninfo'
output.each do |line|
puts line
end
end
end
While possible there is a much better solution - use the crosstab function from the tablefunc extension which can be used with ActiveRecord::Base.connection.execute.
I managed to do this with "\ir" option provided by psql. By this option, you can run the commands written in a file. So put all commands that you want to run in CLI in a single file.
Now you just have to connect to psql and pass filename to "\i" option.
Example: I created a file "a.rb" and it inlcudes the following-
psql -h localhost -U postgres
\ir filename

Connect to heroku apps mongodb (compose) on terminal

I have inherited a Ruby on rails app, which runs on Heroku and has a compose mongoDB. To be clear I have never used ruby, mongo or Heroku and its been a massive learning curve.
I essentially want to get a copy running locally, but more importantly I want to pull down and export the data that is in the db at the moment.
I have installed the latest version of Heroku Toolbelt and I have installed the latest version on mongodb, which also included all 4 packages. Im running this locally on Ubuntu 14 lts.
The documentation I do have says to run this command to log into the db
mongodb://heroku:password-string#tempest.mongohq.com:10098/appid
But if I run just the above it says no such file or directory, if I add 'mongo' onto the beginning then it seems to work but throws the following error
E QUERY Error: More than one ':' detected. If this is an ipv6 address, it needs to be surrounded by '[' and ']'; heroku:password-string#tempest.mongohq.com:10098
I did try running it wrapped in [] too.
This is just getting in so I can then export the data locally and then try to look at it.
The command-line mongo tool doesn't understand URLs, so try this instead:
mongo -u heroku -p password-string tempest.mongohq.com:10098/appid
Also I'm pretty sure you can leave off the -p password-string and it will prompt you for it, so you can avoid revealing it in ps output etc.

Could not connect to a primary node for replica set <Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>

I'm following though with the RailsApp tutorial with Devise and Mongoid (http://railsapps.github.io/tutorial-rails-mongoid-devise.html) and am encountering the following error when I get to 'Rake db:seed' down at the 'Set Up a Database Seed File' section.
Could not connect to a primary node for replica set <Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>
I've tried the instructions from nixoncd on this page here but has not fixed the issue. It tells me 'file exists' and 'Already loaded'. 'https://groups.google.com/forum/#!topic/mongodb-user/Hhh8iNCciMk
I get this if I type 'mongod' in terminal.
ERROR: could not read from config file
Any help welcome. I'm on a Mac OSX Mountain Lion with Mongoid installed using homebrew - though MongoDB was installed using the download package mongodb.org.
MongoDB shell version: 2.4.6
Thanks
EDIT: I'm not sure if this issue is related or not. Also having issues launching mongoDB. Also posted issue here:
mongoDB, could not read from config file -- config in different folder / Uninstall it?
First See if your database is running by mongo , If yes
Use this command:
sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
Your database will work.
Installing MongoDB solved this for me:
sudo apt-get install mongodb-server
The answers above will work for you in the majority of the cases where this error occurs.
However, I would like to note that you can also get the Could not connect to a primary node for replica set error when trying to write exceptionally large batches of records to MongoDB in one request. I have encountered this error when writing more than 200,000 1 KB documents to a remote MongoDB server with in one request. The remote server had 8 GB memory and would handle several requests at once. This error can be avoided by cutting down the batch size of your requests.

Postgres Server error -> PGError: could not connect to server

I get the error below when trying to start my rails app on the localhost:
PGError (could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
From what I have read it sounds like this is most likely a problem in connecting to the Postgres server, and may indicate that it is not started?
It all started when I was attempting my first (yay noobs!) merge using git. There was a conflict (having to do with the Rubymine workspace.xml file), and I started to open up a conflict resolution program. It then seemed that there was really no need to track workspace.xml at all and so I quit from the resolution program, intending to run "git rm --cached" on the file. I think in quitting the program something went foul, and I ended up restarting, before untracking the file, and then completing the merge. Further evidence that something was gummed up is that my terminal shell didn't open up correctly until I restarted the machine.
Now, as far as I can tell, everything in the merge went fine (there were trivial differences in the two branches anyway), but for the life of me I can't seem to get rid of this PGError. If it is as simple as starting the server, then I'd love help on how to do that.
(other context: OSx, rails 3, posgresql83 installed via macports, pg gem).
EDIT - I have been trying to start up the server, but am not succeeding. e.g., I tried:
pg_ctl start -D /opt/local/var/db/postgresql83/defaultdb
This seems to be the right path for the data (it finds the postgresql.conf file) but the response I get is "cannot execute binary file."
Try sudo port load postgresql83-server - this should work with the latest 8.3 port from macports.
If this doesn't work, try sudo port selfupdate; sudo port upgrade outdated and then try again.
Note - this may take absolutely ages.
and may indicate that it is not started?
Yes, sounds like the server is not running on your local machine.
See the description of this error in the PostgreSQL manual:
http://www.postgresql.org/docs/8.3/static/server-start.html#CLIENT-CONNECTION-PROBLEMS
To start the server, try something along the following lines (adjust for pgsql version # and logfile):
sudo su postgres -c '/opt/local/lib/postgresql84/bin/pg_ctl -D /opt/local/var/db/postgresql8/defaultdb -l /opt/local/var/log/postgresql84/postgres.log start'
To stop the server,
sudo su postgres -c '/opt/local/lib/postgresql84/bin/pg_ctl -D /opt/local/var/db/postgresql84/defaultdb stop'

Mongodb, sharding and multiple windows services

In order to get sharding to work I need to run two copies of mongod.exe. One as a shard and one as the config server. How can I install both mongod instances as windows services?
The following command line will install a 2nd instance of Mongo DB. Note that you have to provide serviceName, serviceDisplayName, port, dbpath and logpath in order to avoid collisions.
mongod --install --serviceName "Mongo DB 2nd instance" --serviceDisplayName "Mongo DB 2nd instance" --port 37017 --dbpath c:\data\db2 --logpath c:\data\logs\mongolog2.txt
Then you just start the service with
net start "Mongo DB 2nd instance"
Use sc.exe from the Windows Resource Kit ( http://support.microsoft.com/kb/251192 ) which allows you to specify an unique display name for each instance:
sc.exe create "Mongo DB 1" binPath= "c:\mongodb\bin\mongod.exe --service --dbpath=c:\data\db --logpath=c:\data\log.txt"
create a new config file with different port, db location, etc.. and save as mongod2.cfg
mongod --install --serviceName 'Mongo DB 2nd instance' --serviceDisplayName 'Mongo DB 2nd instance' -f 'C:\Program Files\MongoDB\Server\4.4\bin\mongod2.cfg'
need to use full path to the new config file
this will install as service and you can edit all configurations, like the main instance
Mongo added a --serviceDisplayName flag in 1.8

Resources