Count document in MongoDB

Use db.collection.count() to count the number of documents. You can add a query filter to count the specific documents. Examples are here.

Remove the last character in Java

StringUtils in apache-common-lang3 framework is an useful class. You can use removeEnd to remove the last character or characters. If you want remove the character and ignore the case use removeEndIngoreCase instead.

Update document in MongoDB

We use db.collection.update() to update the document. Since version 3.2 you can use db.collection.updateOne() ,db.collection.updateMany() or db.collection.replaceOne() to update document or documents. Click here for more detail. In this page I will show you simple examples.

Encoding(garbled) problem in SpringMVC tomcat

When I use SpringMVC and tomcat to develop the project I often meet garbled problem. In this page I will show you the right way to use them. The most important thing is making the "input" and "output" encoding same.

Use IDEA Mybatis plugin generate mapper files

Mybatis is a light weight Java persistence framework(compared with Hibernate). You have to write a lot of mapper xml files and mapper interfaces. These things are duplicate works, we can generate them by IDEA Mybatis plugin. In this page I will show you how to use this plugin to generate xml mapper file and mapper interface.

Spring boot scheduled tasks example

In this page I will show you how to use scheduling tasks with Spring Boot. As we all known there is no config file in Spring Boot. We use annotations to config scheduling tasks. This example also works well in Spring.

Count the number of output lines in Linux

If the output or a text file is not too long we can know how many lines. But sometimes the output is to long to show them in one screen. At this point you need wc command to count the numbers of output lines. For example there are many log files like following.

Spring MongoTemplate example

MongoTemplate can help you CURD documents in MongoDB easily. In this page I will show you how to use it. I would recommend you to learn Mongo tutorial first, if you are unfamiliar with commends in MongoDB. For quick start I use Spring Boot to test MongoTemplate.

Spring boot parse POST json example

You can post a form to server and parse the data. Json is a lightweight data format, it's better to post json when your data is complex. In the page I will show you how to parse json data in Spring Boot. You can use Postman or other REST clients to send a POST request. I prefer to use REST Client in Intellij IDEA. Click Use Intellij IDEA REST Client POST json for more detail.

Use Intellij IDEA REST Client POST json

If you develop RESTful Web Service you can use REST Client to test it. The tool can help you submitting a request. It is in Tools | Test RESTful Web Service. After opening the tool you can choose the HTTP method. This tool support common methods(POST,GET,DELETE etc.). In this page I will show you how to use it POST json.

Common Content-Type when POST data

There are 3 common Content-Type when POST data to server. We use Content-Type to tell server the type of data. This page will list 3 common Content-Type in POST.

Difference between save and insert in MongoDB

Insert data into MongoDB is very easy. In this page I will tell you the difference between save method and insert method in MongoDB. If you don't know how to insert data into MongoDB you can see this blog(Insert data in MongoDB). To put it simply, save method will call insert or update method. It is depends on the data you saved contain the _id or not. If the data you saved contain the _id save method will call update method. If not it will call insert method. Examples are here.

Create and drop index in MongoDB

Using db.collection.createIndex(keys, options) to create index for a field. If you want create an ascending index on a filed you need specify a value of 1; for descending index use -1. Examples are here.

Spring MongoRepository example

In this page I will show you how to use MongoRepository insert, delete, update and query document. I assume you have installed MongoDB in your computer. If you don't know how to work with MongoDB you can learn basic methods from MongoDB tutorial. MongoRepository is a interface which can help you CRUD document in MongoDB.

Delete document in MongoDB

Use db.collection.remove() method to delete document in MongoDB. You can define a query to make it delete documents match query. How to define a query click Query document in MongoDB. If you want remove all documents of this collection just use empty query to match all documents. MongoDB will delete all document of this collection.