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 – Where is the log file?

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 – bind() failed errno:99 Cannot assign requested address for socket

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 MongoDB – Failed to unlink socket file /tmp/mongodb-27017

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 Spring Data MongoDB + JSR-310 or Java 8 new Date APIs

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 MongoDB : Sort exceeded memory limit of 104857600 bytes

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 – Aggregation Grouping Example

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 Spring Data MongoDB – Select fields to return

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 – Update to upper case

This article shows you 2 ways to export a MongoDB “aggregation” result into another new collection. 1. $out Example This $out operator is New in version 2.6. 1.1 Review a simple grouping example, it writes the result to a new variable “result”. > var result = db.hc_hosting.aggregate( { $group : { _id : "$hosting", total […]

Read more MongoDB : write Aggregation result into a new collection

A collection with NumberLong type data as follows : > db.hc_whois.findOne(); { "_id" : NumberLong(3000001), "startIpInLong" : NumberLong(1543503872), "endIpInLong" : NumberLong(1544552447), "name" : "ap-net-1", //… } Export it with mongoexport : server1 $ mongoexport -d mydb -c hc_whois -o whois.json whois.json { "_id" : { "$numberLong" : "3000001" }, "startIpInLong" : { "$numberLong" : "1543503872" […]

Read more mongoimport unable to import $numberLong

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 MongoDB – Aggregate and Group example

Review following documents : Collection : domain { "_id" : 1001, "domainName" : "google.com" "tag" : [ "search engine", "search", "find anything", "giant" ] }, { "_id" : 1002, "domainName" : "yahoo.com" "tag" : [ "search engine", "online portal", ] }, { "_id" : 1003, "domainName" : "mkyong.com" } 1. Question How to find all […]

Read more MongoDB – find all documents where an array / list size is greater than N

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 Spring Data MongoDB : get last modified records (date sorting)

In SQL, the ‘like’ query is looks like this : select * from tags where tagName like ‘%apple%’ In MongoDB console, it looks like this : db.tags.find({"tagName": /apple/}) In Spring data mongodb, it implements with Criteria or BasicQuery : String tagName = "apple"; Query query = new Query(); query.limit(10); query.addCriteria(Criteria.where("tagName").regex(tagName)); mongoOperation.find(query, Tags.class); String tagName = […]

Read more Spring Data MongoDB : like query example

Using Spring Data MongoDB 1.2.1.RELEASE and Spring core 3.2.2.RELEASE, while system is starting, it hits some weird spring-asm IncompatibleClassChangeError errors : java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class pom.xml <properties> <spring.version>3.2.2.RELEASE</spring.version> <springdata.version>1.2.1.RELEASE</springdata.version> </properties> <dependencies> <!– Spring Core –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!– Spring Data for MongoDB –> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>${springdata.version}</version> […]

Read more Spring asm dependency issue in Spring Data

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 Spring Data MongoDB : Save binary file, GridFS example

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 MongoDB : couldn’t open /data/db/yourdb.ns errno:13 Permission denied

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 Spring Data MongoDB remove _class column

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 How to install mongoDB on Ubuntu

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 : Delete 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 : Insert document

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 Spring Data MongoDB hello world example

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 : Convert JSON data to DBObject

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 Java MongoDB : Get collection from database