MongoDB on Simple Hosting

MongoDB is an up-and-coming NO-SQL database that uses JSON to store and retrieve data. The flexibility and speed it offers in some use cases has lead to a steady increase in developer adoption, especially paired with Node.js.

MongoDB 2.4 is currently available on Simple Hosting and can be used with all languages.

You can create as many databases and users you want in your instance. You are only limited by the disk size, which you can increase at any time.

The MongoDB database service can be managed from the console or from a Web interface. This article describes how to access, create and manage MongoDB databases on Simple Hosting.

Connecting to your MongoDB database

The MongoDB database service is available on localhost at the default port 27017. It can only be access from with the instance and cannot be reached from the outside world. No username or password are so necessary to quickly test your connection and perform management tasks.

Default connection settings:

  Host: localhost
  Port: 27017
  User: <none>
  Password: <none>
  Database: <any>

The URL to connect to a database named myapp-production would look like this:

  mongodb://localhost:27017/myapp-production

You are encouraged to create new users with strong credentials for your application. You'll find instructions on how to perform management tasks below.

Managing your MongoDB database with RockMongo

You can access your Simple Hosting database by clicking on the “login” link that you see corresponding to your database in the 'Access' section of your instances' management page:

When you click on the link to login, you will then enter your Simple Hosting instance ID number and the admin password of your instance (the one that you specified when you created it). When done, you will then see the RockMongo interface.

By default there is no user account or password.

Create a MongoDB database

From the RockMongo interface, click on the 'Databases' link at the top of the page, then click on 'Create new Database'. Enter a name for your database, and then click on the 'Create' button. The new database will appear in the left-hand pane.

Create a database collection

To create a new database collection, click on your new database and then click on 'New Collection' at the top of the page. Type in a name for your collection, and then click on the 'Create' button.

Create a new document

To create a new document within a collection, click on the collection under which you want to add documents. When you click on a collection you will see all the documents within that collection. To create a new document, click on the 'Insert' link at the top. You can enter the document's data either in JSON or array format and click on 'Save'.

Exporting an existing database

From the RockMongo interface, select the database you want to export. Click on the 'Export' link at the top of the page. Check the boxes next to the collections you would like to export, whether you want the exported file to be compressed, and then click on the 'Export' button. The exported file should begin downloading in your browser.

Importing an existing database

From the RockMongo interface, select or create a database you want to import your data into. Click on the 'Import' link at the top of the page. Then browse for the .js file to import, or the .json file containing a specific collection you'd like to import. Next, click on the 'Import' button to import the data.

Managing your MongoDB database from the command line

To access your MongoDB database by command line, you must first log into your instance via the SSH console .

Note that the console will automatically disconnect after a few minutes of inactivity. If this happens, you can just initiate a new SSH connection without needing to reactivate the console.

Accessing your database

Launch the mongo shell with the following command:

$ mongo
MongoDB shell version: 2.4.8
connecting to: test
Welcome to the MongoDB shell.
>

Switch to the database you would like to perform operation on with the 'use' command:

> use database_name
switched to db database_name

Importing / Exporting a database

To export a database from the SSH console, run the command 'mongodump' and specify which database/collection you'd like to export, and a writable directory to output the file:

# Export entire database
$ mongodump --db database_name --out /srv/data/tmp/mongodump-yyyy-mm-dd

# Export a collection within a database
$ mongodump --collection collection_name --db database_name --out /srv/data/tmp/mongodump-yyyy-mm-dd

Note that only the '/srv/data/tmp' directory can be written to - with the exception of virtualhosts. Consequently, we recommend creating a specific directory for MongoDB exports in order to gather them together in the same location.

To import a database from the SSH console, run the command 'mongoimport' and specify which database/collection you'd like to import the data into, along with the file containing the data to import:

$ mongoimport --db database_name --collection collection_name --file collection.json

Enabling full-text search requires setting an administrative parameter in the default admin database.

From the mongo shell you can list all administrative parameters with the following commands:

> use admin
switched to db admin
> db.runCommand( { getParameter: '*' } )
{
        "enableLocalhostAuthBypass" : true,
        "enableTestCommands" : 0,
        "logLevel" : 0,
        "logUserIds" : false,
        "notablescan" : false,
        "quiet" : false,
        "releaseConnectionsAfterResponse" : false,
        "replApplyBatchSize" : 1,
        "replIndexPrefetch" : "uninitialized",
        "supportCompatibilityFormPrivilegeDocuments" : true,
        "syncdelay" : 60,
        "textSearchEnabled" : false,
        "ttlMonitorEnabled" : true,
        "ok" : 1
}

You can then enable full-text search by setting the value of textSearchEnabled to 'true':

> db.runCommand ( { setParameter: 1, textSearchEnabled: true } )
{ "was" : false, "ok" : 1 }

See also

Last modified: 11/14/2017 at 20:49 by Richard M. (Gandi)