In the previous blog, we successfully installed and started the Mongo database. In this blog, I will introduce the development of the MongoDB database. For details, refer to the official MongoDB documentation. To smoothly develop MongoDB databases, follow these steps:
1. Download the Mongo database driver. The driver query is very different from the JDBC driver.
Click the address https://github.com/mongodb/mongo-java-driver/downloads to enter the MongoDB java driver download page, download the latest driver.
2. Create a java project, such as MongoDB, create a package com. geostar. mongo. test, and create a class MongoDB.
The MongoDB class is very simple. There is only one main function. Let's just test the MongoDB database.
View Code
ackage com.geostar.mongo.test;
import java.net.UnknownHostException;
import java.util.List;
import com.mongodb.Mongo;
public class MongoDB {
//------MongoDB java driver https://github.com/mongodb/mongo-java-driver/downloads
/**
* @param args
*/
public static void main(String[] args)throws UnknownHostException {
// TODO Auto-generated method stub
Mongo mongo=new Mongo("localhost",27017);
List<String> dbs=mongo.getDatabaseNames();
if(dbs!=null){
for(String name:dbs){
System.out.println("Mongo db name:"+name);
};
};
}
}
Right-click> Run As> Java Application, Run the program, and the console can output:
Mongo db name: local
This is just a simple example, but I think it is okay to get started. If you want to explore its powerful functions, you can continue to explore MongoDB and use the MongoDB database driver to operate the database, you can also use Spring Mongo in the Spring framework to do something complicated.