Mongo DB - Yash-777/mongo-java-driver GitHub Wiki
MongoDB is an open-source document database and leading NoSQL database. The MongoDB Server is written in C++ with an extensive suite of tests in JavaScript. MongoDB uses SCons to build the Server and the shell.
Download the latest production release of Server and install it.
Run MongoDB Server in Secure Mode: The database does not enable “Secure Mode” by default. To start MongoDB, run mongod.exe from a Command Prompt.
-
By default, MongoDB listens for connections from clients on port 27017, and stores data in the /data/db directory.
-
Create Data directory: MongoDB requires a data directory to store all data. MongoDB’s default data directory path is
\data\db
. Create this folder using the following commands from a Command Prompt:
md \data\db
-
If you want mongod to store data files at a path other than
/data/db
you can specify a dbPath. The dbPath must exist before you start mongod. If it does not exist, create the directory and the permissions so that mongod can read and write data to this path. -
Either you can use the default port number or you can Specify a TCP Port by using the --port option
mongod.exe --port 27017 --logpath /var/log/mongodb.log --dbpath C:\data\db
This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe
process is running successfully.
Connect to MongoDB Server through any of below specified:
1 Mongo Shell: To connect to MongoDB through the shell, Then in another shell run mongo.exe
C:\Program Files\MongoDB\Server\3.2\bin\mongo.exe
2 RoboMongo Client: Create a new connection, set the name, IP address and the appropriate port, Setup authentication, if required
3 MongoDB Java driver: Chose an appropriate driver that connect to the server. To a Java Language use Mongo Java Driver which provides both synchronous and asynchronous interaction with MongoDB.
host « The host-name of the machine mongodb is running on (defaults to localhost).
username « The username to use when authenticating to mongod (only use if running with auth).
password « The password to use when authenticating to mongod (only use if running with auth).
db « The database to authenticate to (only necessary if running with auth).
JSON: JavaScript Object Notation. A human-readable, plain text format for expressing structured data with support in many programming languages. A JSON document is a collection of fields and values in a structured format.
MongoDB Extended JSON « JSON can only represent a subset of the types supported by BSON.
BSON: A serialization format used to store documents and make remote procedure calls in MongoDB. “BSON” is a portmanteau of the words “binary” and “JSON”. Think of BSON as a binary representation of JSON (JavaScript Object Notation) documents.
Example to Insert Record:
public static void insertBSONRecord(String key1, String value1) {
Mongo mongoClient = null;
try {
mongoClient = new MongoClient( mongoDBHost, mongoDBPort ); // Connect to MongoDB
DB db = mongoClient.getDB( mongoDBName ); // Get database
boolean auth = db.authenticate(mongoDBUserName, mongoDBPassword.toCharArray());
System.out.println("Mongo DB Authentication >>> "+auth);
if( auth) {
DBCollection table = db.getCollection( mongoDB_CollectionName );
BasicDBObject document = new BasicDBObject();
document.put(key1, value1 );
table.insert(document);
String id = document.get("_id").toString();
System.out.println("Inserted ID : "+id);
BasicDBObject document = new BasicDBObject("_id",new ObjectId( id ));
DBCursor cursor = table.find(document);
if ( cursor.hasNext() ){
DBObject next = cursor.next();
String Key1 = (String) next.get("key1");
}
}
mongoClient.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} finally {
if( mongoClient != null ) mongoClient.close();
}
}