MongoDB – Where is the log file?

Default, MongoDB creates the log file at this path /var/log/mongodb/mongodb.log, if the log file is not found, please check with the MongoDB config file. logpath Check the MongoDB config file at /etc/mongod.conf or /yourMongoDBpath/mongod.conf, the logpath defined where to log. /etc/mongod.conf $ cat /etc/mongod.conf # mongod.conf # Where to store the data. # Note: if …

Read more

MongoDB – bind() failed errno:99 Cannot assign requested address for socket

Review the server environment, we need to allow Server B to access the Server A MongoDB database. Server A – MongoDB server Private IP – 192.168.162.129 / 17 Server B – Application Server Private IP – 192.168.204.205 / 17 Update the bind_ip, but unable to start mongod process anymore. /etc/mongod.conf #$ vim /etc/mongod.conf # Listen …

Read more

MongoDB – Failed to unlink socket file /tmp/mongodb-27017

Upgraded MongoDB and unable to start the mongod process, review the /var/log/mongodb/mongod.log file, and found the following error messages : Terminal 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] MongoDB starting : pid=31927 port=27017 dbpath=/var/lib/mongodb3 64-bit host=hcompass 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] db version v3.2.16 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] git version: 056bf45128114e44c5358c7a8776fb582363e094 2017-08-24T03:57:21.289-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t …

Read more

Spring Data – Add custom method to Repository

In this article, we will show you how to add a custom method to Spring Data JPA CrudRepository and MongoDB MongoRepository 1. CrudRepository 1.1 Review a CustomerRepository, we will add a custom method to this repository. CustomerRepository.java package com.mkyong.dao; import com.mkyong.model.Customer; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import java.util.Date; import java.util.List; import java.util.stream.Stream; public interface …

Read more

Spring Boot + Spring Data MongoDB example

In this article, we will show you how to create a Spring Boot + Spring Data MongoDB application, using Gradle build tool. Spring Boot 1.5.1.RELEASE MongoDB Gradle Java 8 1. Project Structure A standard project structure. 2. Project Dependency 2.1 A Gradle build file. build.gradle buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE") } …

Read more

Spring Data MongoDB + JSR-310 or Java 8 new Date APIs

While saving an object containing the new Java 8 java.time.LocalDateTime, the following error is thrown : org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date] Tested Spring 4.3.2.RELEASE Spring Data MongoDB 1.9.2.RELEASE Is Spring-data supporting the new Java 8 Date APIs (JSR-310)? 1. Spring Data + JSR-310 Yes, Spring-data supports the …

Read more

MongoDB – group, count and sort example

Some MongoDB examples to show you how to perform group by, count and sort query. 1. Test Data A whois_range collection, containing many records. > db.whois_range.find(); { "_id" : 1, "country" : "us", "source" : "ARIN", "status" : "NEW", "createdDate" : ISODate("2016-05-03T08:52:32.434Z") }, { "_id" : 2, "country" : "us", "source" : "ARIN", "status" : …

Read more

MongoDB : Sort exceeded memory limit of 104857600 bytes

Performs a large sort operation (Aggregation) in a collection, and hits the following error message : MongoDB Console > db.bigdata.aggregate( {$group : {_id : "$range", total : { $sum : 1 }}}, {$sort : {total : -1}} ); #… aggregate failed at Error (<anonymous>) at doassert (src/mongo/shell/assert.js:11:14) #… Error: command failed: { "errmsg" : "exception: …

Read more

Spring Data MongoDB – Aggregation Grouping Example

In this tutorial, we will show you how to do the data grouping with Spring Data + MongoDB aggregation framework. 1. Test Data domain.json { "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" } { "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com"} { "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" …

Read more

Spring Data MongoDB – Select fields to return

In MongoDB console, you can use field:1 to select the fields to return from a query : > db.hosting.find({},{domain:1, count:1}); In Spring Data for MongoDB, you use query.fields().include : HostingDaoImpl.java package com.mkyong.core.hosting.dao; import java.util.List; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Repository; import com.hostingcompass.core.db.dao.MongoDaoImpl; @Repository public class HostingDaoImpl extends MongoDaoImpl<Hosting> implements HostingDao { …

Read more

MongoDB – Allow remote access

In this tutorial, we will show you how to enable remote access to a MongoDB server. Here is the tested environment : 1. MongoDB Server Private IP – 192.168.161.100 Public IP – 45.56.65.100 MongoDB 2.6.3, port 27017 IpTables Firewall 2. Application Server (Same LAN network) Private IP – 192.168.161.200 Public IP – irrelevant 3. Developers …

Read more

MongoDB – Update to upper case

A document, and you want to update all the ‘source’ values to UPPERCASE. whois.json { “_id” : NumberLong(1), “country” : “au”, “source” : “apnic” } { “_id” : NumberLong(2), “country” : “cn”, “source” : “apnic” } { “_id” : NumberLong(3), “country” : “us”, “source” : “arin” } Solution No sure if there is any ready …

Read more

MongoDB – Remove a field from array

Prior to MongoDB 2.6, there is no official function to $unset a field from array document. Note $unset is not working with arrays $pull is used to remove the entire record, not a particular field. 1. Arrays Documents In this article, we will show you how to remove the field “countryName” from below array. > …

Read more

MongoDB – Aggregate and Group example

In this tutorial, we will show you how to use MongoDB aggregate function to group documents (data). 1. Test Data Data in JSON format, shows the hosting provider for website. website.json { “_id” : 1, “domainName” : “test1.com”, “hosting” : “hostgator.com” } { “_id” : 2, “domainName” : “test2.com”, “hosting” : “aws.amazon.com”} { “_id” : …

Read more

Spring Data MongoDB : get last modified records (date sorting)

In Mongodb, you can use sort()to do the date sorting. 1. MongoDB Sorting Examples In Mongodb, to sort a “date” field, issues : //The ‘1’ = sort ascending (oldest to newest) db.domain.find().sort({lastModifiedDate:1}) { "_id" : 1, "lastModifiedDate" : ISODate("2013-08-13T07:18:04.774Z") } { "_id" : 2, "lastModifiedDate" : ISODate("2013-09-03T08:12:16.309Z") } { "_id" : 3, "lastModifiedDate" : ISODate("2013-10-03T08:12:16.309Z") …

Read more

MongoDB import and export example

In this tutorial, we show you how to backup and restore MongoDB with the commands : mongoexport and mongoimport. 1. Backup database with mongoexport Few examples to show you how to use the mongoexport to back up the database. Review some of the common use options. $ mongoexport Export MongoDB data to CSV, TSV or …

Read more

Spring Data MongoDB : Save binary file, GridFS example

In MongoDB, you can use GridFS to store binary files. In this tutorial, we show you how to use Spring Data’s GridFsTemplate to store / read image in / from MongoDB. 1. GridFS – Save example (Spring config in Annotation) Gat an image file and save it into MongoDB. SpringMongoConfig.java package com.mkyong.config; import org.springframework.context.annotation.Bean; import …

Read more

MongoDB Authentication example

This guide shows you how to enable authentication in MongoDB. The authentication is disabled by default. To configure it, you must first add a user to the “admin” database. > show dbs admin #add single user to this database testdb Note Users with normal access in “admin” database, HAVE read and write access to all …

Read more

MongoDB : couldn’t open /data/db/yourdb.ns errno:13 Permission denied

Starting MongoDB server, it shows error “Permission denied” on one of the database and shutdown the server automatically. $ mongod Fri Mar 8 22:54:46 [initandlisten] MongoDB starting : pid=13492 port=27017 dbpath=/data/db/ 64-bit host=Yongs-MacBook-Air.local //… Fri Mar 8 22:54:46 [initandlisten] journal dir=/data/db/journal Fri Mar 8 22:54:46 [initandlisten] recover : no journal files present, no recovery needed …

Read more

How to install MongoDB on Mac OS X

A guide to show you how to install MongoDB on Mac OS X. MongoDB 2.2.3 Mac OS X 10.8.2 1. Download MongoDB Get MongoDB from official website, extracts it : $ cd ~/Download $ tar xzf mongodb-osx-x86_64-2.2.3.tgz $ sudo mv mongodb-osx-x86_64-2.2.3 /usr/local/mongodb 2. MongoDB Data By default, MongoDB write/store data into the /data/db folder, you …

Read more

How to set environment variables on Mac OS X

In Mac OS X, you can set the environment variables in one of the following files : ~/.bashrc ~/.bash_profile ~/.profile By default, Mac OS X does not has above files, you need to create it manually. $PATH example This example shows you how to set “mongodb/bin” folder to the existing $PATH environment variable. $ echo …

Read more

Due to limitations of the BasicDBObject, you can’t add a second ‘$and’

Problem Using Spring data and Mongodb, below is a function to find data within a date range. public List<RequestAudit> findByIpAndDate(String ip, Date startDate, Date endDate) { Query query = new Query( Criteria.where("ip").is(ip) .andOperator(Criteria.where("createdDate").gte(startDate)) .andOperator(Criteria.where("createdDate").lt(endDate)) ); return mongoOperation.find(query, RequestAudit.class); } It hits following error message : org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can’t add …

Read more

Spring Data MongoDB remove _class column

By default, SpringData’s MappingMongoConverter add an extra “_class” column for every object saved in MongoDB. For example, public class User { String username; String password; //…getters and setters } Save it MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate"); User user = new User("mkyong", "password123"); mongoOperation.save(user, "users"); Result > db.users.find() { "_class" : "com.mkyong.user.User", "_id" : ObjectId("5050aef830041f24ff2bd16e"), "password" : …

Read more

MongoDB hello world example

A quick guide to show you how to do basic operations like create, update, find, delete record and indexing in MongoDB. This example is using MongoDB 2.0.7, running on Mac OS X 10.8, both MongoDB client and server console are run on localhost, same machine. 1. Install MongoDB Install MongoDB on Windows, Ubuntu or Mac …

Read more

How to install mongoDB on Ubuntu

This guide shows you how to install MongoDB on Ubuntu. Ubuntu 12.10 MongoDB 2.2.3 1. Add 10gen package to source.list.d Ubuntu 12 comes with a “mongo” package, but not the latest version. $ sudo apt-cache search mongodb mongodb mongodb-clients mongodb-dev mongodb-server It’s recommended to add 10gen package to /etc/apt/sources.list.d, as it contains the latest stable …

Read more

Spring Data MongoDB : Delete document

In Spring data for MongoDB, you can use remove() and findAndRemove() to delete documents from MongoDB. remove() – delete single or multiple documents. findAndRemove() – delete single document, and returns the deleted document. Common Mistake Don’t use findAndRemove() to perform a batch delete (remove multiple documents), only the first document that matches the query will …

Read more

Spring Data MongoDB : Update document

In Spring data – MongoDB, you can use following methods to update documents. save – Update the whole object, if “_id” is present, perform an update, else insert it. updateFirst – Updates the first document that matches the query. updateMulti – Updates all documents that match the query. Upserting – If no document that matches …

Read more

Spring Data MongoDB : Insert document

In Spring data MongoDB, you can use save(), insert() to save a or a list of objects into mongoDB database. User user = new User("…"); //save user object into "user" collection / table //class name will be used as collection name mongoOperation.save(user); //save user object into "tableA" collection mongoOperation.save(user,"tableA"); //insert user object into "user" collection …

Read more

Spring Data MongoDB hello world example

In this tutorial, we show you how to use “SpringData for MongoDB” framework, to perform CRUD operations in MongoDB, via Spring’s annotation and XML schema. Updated on 1/04/2013 Article is updated to use latest SpringData v 1.2.0.RELEASE, it was v1.0.0.M2. Tools and technologies used : Spring Data MongoDB – 1.2.0.RELEASE Spring Core – 3.2.2.RELEASE Java …

Read more

Java MongoDB : Convert JSON data to DBObject

MongoDB comes with “com.mongodb.util.JSON” class to convert JSON data directly to a DBObject. For example, data represent in JSON format : { ‘name’ : ‘mkyong’, ‘age’ : 30 } To convert it to DBObject, you can code like this : DBObject dbObject = (DBObject) JSON.parse("{‘name’:’mkyong’, ‘age’:30}"); Example See a full example to convert above JSON …

Read more

Java MongoDB : Save image example

In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS APIs are able to serve other binary files as well, like video and music files. Note For detail explanation, read this MongoDB GridFS manual. 1. Save image Code snippets to save an image file into MongoDB, …

Read more

Java MongoDB : Update document

In this tutorial, we show you how to use Java MongoDB API collection.update() to update documents. Test Data Assume following data / documents are inserted. { "hosting" : "hostA", "type" : "vps", "clients" : 1000 }, { "hosting" : "hostB", "type" : "dedicated server", "clients" : 100 }, { "hosting" : "hostC", "type" : "vps", …

Read more

Java MongoDB : Query document

In this tutorial, we show you few common ways to get or query document from collection. Test Data Insert 5 dummy documents for testing. { "_id" : { "$oid" : "id"} , "number" : 1 , "name" : "mkyong-1"} { "_id" : { "$oid" : "id"} , "number" : 2 , "name" : "mkyong-2"} { …

Read more

Java MongoDB : Delete document

In this tutorial, we show you how to use collection.remove() to delete documents from the collection. 1. Test Data Insert 10 documents from number 1 to 10 for testing. for (int i=1; i <= 10; i++) { collection.insert(new BasicDBObject().append("number", i)); } 2. DBCollection.remove() See below code snippets to delete documents. Example 1 Get first document …

Read more