Setup Docker Mongo and Import JSON Data

Setting up docker MongoDB and importing JSON is easy. Just follow these steps.

How to Setup Docker Mongo

  1. I’m assuming you’ve already got Docker installed on your system. if you don’t, do that first.
  2. For the following steps, do not actually run the command until the instruction says so. This is going through the building of the command for your own information.
  3. to get docker mongo from the MongoDB main docker repository, refer to the following:
    docker run --name mongo -d mongo
  4. We will add exposure to both the mongodb connector plugin port, 27017, and the web interface port, 28017, which requires the –httpinterface flag.
    docker run --name mongo -p 27017:27017 -p 28017:28017 --httpinterface
  5. Now we add a mount to a local volume, so you can have local access to the data files created by mongo. Also, just to be nice to the filesystem, we’ll enable smallfiles mode.
    docker run --name mongo -p 27017:27017 -p 28017:28017 -v /my/own/datadir:/data/db -d mongo --httpinterface --smallfiles
  6. Now, also, lets mount the directory on your local system where your CSV data file lives:
    docker run --name mongo -p 27017:27017 -p 28017:28017 -v /my/own/datadir:/data/db -v /path/to/sample/data/file:/data/seeds -d mongo --httpinterface --smallfiles
  7. Finally, hit enter, and run the command from step 6

Go inside the running Docker Mongo instance so you can import data

sudo docker exec -i -t mongo bash

Now, you’re inside the Docker instance and you have a command line waiting for input.

Run in the import of your JSON Data

mongoimport --db someDatabaseName --collection someCollectionName --type --file /data/seeds/yourfile.json

Note: the –db name and the –collection name provided doesn’t need to already exist. They will be automatically created upon execution of the import command

To import an array of JSON objects, just add --jsonArray

mongoimport --db someDatabaseName --collection someCollectionName --type --file /data/seeds/yourfile.json --jsonArray

Mongoose.js Search With Full Text Search

If you’ve tried to use Regular Expressions to search for a string of text in Mongo, you’ve probably noticed that it is really slow.

Use “full text search” instead! The end result will be similar to using a  “LIKE” query in SQL.

NOTE: In order to use this feature, you must create a text index on a field in your data.

How to Make Text Search Really Fast in MongoDB

var name = 'Peter';
model.find({ $text: { $search: name } }, function(err, doc) {
  //Do your action here..
});

How to Use Full Text Search with Exec

model.find({ $text: { $search: q } })
 .limit(20)
 .exec(function (err, items) {
    console.log("items", items);
});

Sources: MongoDB & Comsysto

How to Append Data to a File in Node.js

In NodeJS, to append data to a a file, you can use a quick and simple method in the “fs” module.

How to Append Data to a File in Node.js

//load the fs module
var fs = require('fs');

fs.appendFile('/path/file.txt', "\nAnother Line.", function (err) {
  if(!err) {
    console.log("file successfully saved.");
  } else {
    console.log("error", err);
  }
});

How to Read Data From a File in Node.js

In NodeJS, to read data from an a file, you can use a quick and simple method in the “fs” module.

How to Read Data From a File in Node.js

//load the fs module
var fs = require('fs');

fs.readFile('/path/file.txt', "utf-8", function (err, data) {
  if(!err) {
    console.log("file successfully saved.");
  } else {
    console.log("error", err);
  }
});

How to Delete a File in Node.js

In NodeJS, to delete a file, you can use a quick and simple method in the “fs” module.

How to Delete a File in Node.js

//load the fs module
var fs = require('fs');

fs.unlink('/path/file.txt', function() {
  console.log("File deleted.");
});

How to Write Data to a File in Node.js

In NodeJS, to write data to a file, you can use a quick and simple method in the “fs” module.

How to write data to a File in Node.js

//load the fs module
var fs = require('fs');

fs.writeFile('/path/file.txt', "fileData", function (err) {
  if(!err) {
    console.log("file successfully saved.");
  } else {
    console.log("error", err);
  }
});

Node.js Create a New File

In NodeJS, to create a new, you can use a quick and simple method in the “fs” module.

How to Create a new File in Node.js

//load the fs module
var fs = require('fs');

fs.writeFile('/path/file.txt', "fileData", function (err) {
  if(!err) {
    console.log("file successfully saved.");
  } else {
    console.log("error", err);
  }
});

Node.js Check if a File Exists Reference

In NodeJS, to check if a file exists, you can use a quick and simple method in the “fs” module.

How to Check if a File Exists in Node.js

//load the fs module
var fs = require('fs');

fs.exists('/path/file.txt', function (exists) {
  if(exists) {
    console.log("file exists");
  } else {
    console.log("file not found");
  }
});

How to Import a database into MySQL

Importing a database dump into MySQL is a quick and easy process. However, there are a couple pitfalls that you should know about.

Here’s how to import a SQL dump into MySQL

Example user, password, and database name:
kris
qwerty12345
myDatabase
  1. If you don’t already have it, Create a SQL dump from the original database that you want to import.
  2. In the new database, create the database you want to import the data into.
    mysql -u kris -p CREATE DATABASE myDatabase

    It will prompt you for a password. Type it in and hit enter.

  3. Once created, import the file:
    mysql -u kris -p myDatabase < /path/to/sql/dump/file.sql

    It will prompt you again for a password.
    Note: this command doesn’t give you any feedback as it is progressing (Unless there is an error, and you’ll then get an error message). Don’t worry.

Be Careful

If you accidentally type a “>” instead of the left-pointing “<” character, it will just hang and not give any feedback, which you will sit and stare at for a moment. Then you will cancel the command with Ctrl-C. Doing this actually corrupts the file, and you will then get the following error message next time you try to import the dump file:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Ctrl-C’ at line 1

If this happens to you, replace the dump file with a new one, because it is corrupted. Then re-run your import with the correct “<” character. :)