MongoDB Java drivers have provided comprehensive operations, divided into three parts:
1. Common user operations
2. Administrator operations
3. oplog operations
You can add an eclipse testng plug-in and import the testng. jar package to the project.
This article describes the usage and precautions.
1. Connect a replica set (replica sets) into D:
The replica set is a mongo solution for stability and can implement read/write splitting. Here we introduce the java connection method of the replica set.
[Java]
- StaticMongo m;
- Static{
- List <ServerAddress> addresslist =NewArrayList <ServerAddress> ();
- Try{
- Addresslist. add (NewServerAddress ("Fig: 10240"));
- Addresslist. add (NewServerAddress ("Fig: 11240"));
- Addresslist. add (NewServerAddress ("Fig: 12240"));
- }Catch(UnknownHostException e ){
- System. err. println ("Address check error .");
- System. exit (-1);
- }
- Optional options =NewUsing options ();
- Options. autoConnectRetry =True;
- Options. connectionsPerHost =20;
- Options. connectTimeout =6000;
- Options. maxAutoConnectRetryTime =12000;
- Options. maxWaitTime =12000;
- Options. socketKeepAlive =True;
- Options. socketTimeout =2000;
- Try{
- M =NewMongo (addresslist, options );
- }Catch(Except exception e ){
- System. err. println ("Mongo create error .");
- System. exit (-1);
- }
- }
Note: The connection method used in the general example is to directly connect to a mongod service, but as a single production environment, the security is obviously insufficient. Therefore, the replica set method is used, and the connection is different from the general method.
[Java]
- PublicMongo (List <ServerAddress> replicaSetSeeds, using options)ThrowsExcept exception
Method.
When options is set according to your needs, there are already many parameter descriptions.
Note that mongo instances use Singleton mode.
2. Obtain the database:
[Java]
- DB db = m. getDB ("Test");
Note: Obtain the database named test for the operation. If the database does not exist, the Operation will create the corresponding database.
3. Get the set:
[Java]
- DBCollection col = db. getCollection ("Offer");
Note: This is a set named offer. It is similar to a db table.
The above three operations are to establish a connection, followed by common operations in db.
4. Query:
[Java]
- DBCursor cursor = col. find ();
- While(Cursor. hasNext ()){
- System. out. println (cursor. next ());
- I ++;
- }
Note: The full-set query outputs each record. Normally, similar operations are not performed. remember two things: find () and cursor.
[Java]
- BasicDBObject query =NewBasicDBObject ();
- Query =NewBasicDBObject ();
- Query. put ("I",NewBasicDBObject ("$ Gt",20). Append ("$ Lte",30));// I. e. 20 <I <= 30
- <Pre name ="Code" Class="Java"> DBCursor cur = coll. find (query );
- While(Cur. hasNext ()){
- System. out. println (cur. next ());
- }
Note: query the offer information of a specified condition.
[Java]
- DBObject tmpdoc = col. findOne (doc );
Note: query a single record. When it is clear that one record has only one record, findOne () is more convenient to avoid the trouble of moving the cursor.
5. Update:
[Java]
- DBObject tmpdoc = col. findOne (doc );
- Tmpdoc. put ("I", (Integer) tmpdoc. get ("I") +1);
- System. out. println ("Update obj :"+ Col. findOne (tmpdoc ));
Note: to update a record, read the entire record and then update some information before writing it, Do not read some updates back.
6. insert:
[Java]
- IntI =1000;
- BasicDBObject doc =NewBasicDBObject ();
- Doc. put ("I", I +1);
- Col. insert (doc );
- System. out. println ("Insert obj :"+ Col. findOne (doc ));
Note: Insert a record {I: 1001}, _ id is automatically generated.
7. Delete:
[Java]
- BasicDBObject doc =NewBasicDBObject ();
- Doc. put ("I", I +1);
- Col. remove (doc );
- System. out. println ("Insert obj :"+ Col. findOne (doc ));
Note: delete a record.