Java MongoDB : Insert a document

In this tutorial, we show you 4 ways to insert below JSON data into a “document“, via Java MongoDB API. Test Data Test data in JSON format. { "database" : "mkyongDB", "table" : "hosting", "detail" : { records : 99, index : "vps_index1", active : "true" } } } 1. BasicDBObject example BasicDBObject document = …

Read more

Java MongoDB : Get collection from database

In Java, you can use db.getCollection(“your collection name”) to get a single collection to use. DBCollection collection = db.getCollection("yourCollection"); If you do not know the collection name, use db.getCollectionNames() to get the entire list of collection names from a selected database. DB db = mongo.getDB("yourdb"); Set<String> collections = db.getCollectionNames(); for (String collectionName : collections) { …

Read more

old lock file: \data\db\mongod.lock, probably means unclean shutdown

When Staring the mongoDB server, it hits below errors, and the mongoDB server is unable to start. ************** old lock file: \data\db\mongod.lock. probably means unclean shutdown recommend removing file and running –repair see: http://dochub.mongodb.org/core/repair for more information ************* Mon May 09 12:37:43 [initandlisten] exception in initAndListen std::exception: old lock file, terminating Mon May 09 12:37:43 …

Read more

Java MongoDB : Authentication example

By default, MongoDB is run in trust environment (authentication with a username and password is NOT required). In this tutorial, we will show you how to start MongoDB in secure mode / enable authentication, and connect with the Java MongoDB driver. 1. Start MongoDB in Secure Mode Start MongoDB with –auth option, now, MongoDB need …

Read more

Java + MongoDB hello world example

A simple Java + MongoDB hello world example – how to connect, create database, collection and document, save, update, remove, get and display document (data). Tools and technologies used : MongoDB 2.2.3 MongoDB-Java-Driver 2.10.1 JDK 1.6 Maven 3.0.3 Eclipse 4.2 P.S Maven and Eclipse are both optional, just my personal favorite development tool. 1. Create …

Read more

How to create database in MongoDB

MongoDB didn’t provides any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will create it on the fly, during the first time you save the value into the defined collection (or table in SQL), and database. For developer from SQL background, we need to create a database, table and …

Read more

How to run MongoDB as Windows Service

This Article is outdated! Please refer to this latest guide to install MongoDB as Windows Service. A guide to show you how to installed MongoDB on Windows. 1. mongod –help Get to know all the Windows service related commands by typing “mongod –help“. C:\MongoDB\bin>mongod –help Windows Service Control Manager options: –install install mongodb service –remove …

Read more

How to install MongoDB on Windows

In this tutorial, we will show you how to install MongoDB on Windows. MongoDB 2.2.3 Windows 7 Note The MongoDB does not require installation, just download and extracts the zip file, configure the data directory and start it with command “mongod“. 1. Download MongoDB Download MongoDB from official MongoDB website. Choose Windows 32 bits or …

Read more