Java operation mongodb implementation CURD function instance

  • 2020-04-01 02:39:37
  • OfStack

To download the corresponding driver: official website to download: (link: http://central.maven.org/maven2/org/mongodb/mongo-java-driver/)
This article USES the mongo-2.10.1.jar version:

The detailed code below allows each method to be executed separately. For the sake of ease of operation, the database is re-linked in each method and not mentioned again, because the purpose here is to understand mongodb.


package com.mongo.dao;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

public class EntityTest {

     public static void main(String[] args) throws Exception{
          delete();
     }

     
     public static void saveEntity() throws Exception{
          //First: instantiate the mongo object, connect the mongodb server & NBSP; Contains all databases

          //The default constructor, the default is to connect to the native, the port number, the default is 27017
          //The equivalent of Mongo Mongo =new Mongo("localhost",27017)
          Mongo mongo =new Mongo();

          //Second: connect to a specific database
          //Where the parameter is the name of the specific database, if the server does not exist, will be automatically created
          DB db=mongo.getDB("myMongo");

          //Third: operate on specific tables
         //There is no concept of tables in mongodb, but collections
          //Where the parameter is a table in the database, if it does not exist, will be automatically created
          DBCollection collection=db.getCollection("user");

          //Add operation
          //There is no concept of rows in mongodb, but documents
          BasicDBObject document=new BasicDBObject();

          document.put("id", 1);
          document.put("name", " Xiao Ming ");
//          // And then save it to the collection 
//     //     collection.insert(document);

         
          //Of course, I can also save this json string

          //The above json string is implemented as follows:
          //The first: when it's like XML, keep adding
          BasicDBObject addressDocument=new BasicDBObject();
          addressDocument.put("city", "beijing");
          addressDocument.put("code", "065000");
          document.put("address", addressDocument);
          //And then save it in the database
          collection.insert(document);

          //The second is to store json directly into the database
    
     }

     
     public static void selectAll() throws Exception{
          //First: instantiate the mongo object, connect the mongodb server & NBSP; Contains all databases

          //The default constructor, the default is to connect to the native, the port number, the default is 27017
          //The equivalent of Mongo Mongo =new Mongo("localhost",27017)
          Mongo mongo =new Mongo();

          //Second: connect to a specific database
          //Where the parameter is the name of the specific database, if the server does not exist, will be automatically created
          DB db=mongo.getDB("myMongo");

          //Third: operate on specific tables
         //There is no concept of tables in mongodb, but collections
          //Where the parameter is a table in the database, if it does not exist, will be automatically created
          DBCollection collection=db.getCollection("user");

          //Query operation
          //Query all
          //It is similar to the concept of cursor in access database
          DBCursor cursor=collection.find();
          System.out.println("mongodb In the user The table results are as follows: ");
          while(cursor.hasNext()){
               System.out.println(cursor.next());
          }
     }

     
     public static void selectPart() throws Exception{
          //First: instantiate the mongo object, connect the mongodb server & NBSP; Contains all databases

          //The default constructor, the default is to connect to the native, the port number, the default is 27017
          //The equivalent of Mongo Mongo =new Mongo("localhost",27017)
          Mongo mongo =new Mongo();

          //Second: connect to a specific database
          //Where the parameter is the name of the specific database, if the server does not exist, will be automatically created
          DB db=mongo.getDB("myMongo");

          //Third: operate on specific tables
         //There is no concept of tables in mongodb, but collections
          //Where the parameter is a table in the database, if it does not exist, will be automatically created
          DBCollection collection=db.getCollection("user");

    
          //You can just put
          BasicDBObject queryObject=new BasicDBObject();
          queryObject.put("id", 1);
          DBCursor querycursor=collection.find(queryObject);
          System.out.println(" The condition query is as follows: ");
          while(querycursor.hasNext()){
               System.out.println(querycursor.next());
          }
     }

     
     public static void update()throws Exception{
          //First: instantiate the mongo object, connect the mongodb server & NBSP; Contains all databases

          //The default constructor, the default is to connect to the native, the port number, the default is 27017
          //The equivalent of Mongo Mongo =new Mongo("localhost",27017)
          Mongo mongo =new Mongo();

          //Second: connect to a specific database
          //Where the parameter is the name of the specific database, if the server does not exist, will be automatically created
          DB db=mongo.getDB("myMongo");

          //Third: operate on specific tables
         //There is no concept of tables in mongodb, but collections
          //Where the parameter is a table in the database, if it does not exist, will be automatically created
          DBCollection collection=db.getCollection("user");

          //The updated object
//                  The first way to update
          BasicDBObject newBasicDBObject =new BasicDBObject();
          newBasicDBObject.put("id", 2);
          newBasicDBObject.put("name", " The little red ");
          collection.update(new BasicDBObject().append("id", 1),newBasicDBObject);

//                  The second way to update
//                  Update a certain field
//                  BasicDBObject newBasicDBObject =new BasicDBObject().append("$set",new BasicDBObject().append("name", "little red "));
//                  Collection.update (new BasicDBObject().append("id", 1).append("name", "xiaoming "),newBasicDBObject);
         
          DBCursor querycursor1=collection.find();
          System.out.println(" The updated results are as follows: ");
          while(querycursor1.hasNext()){
               System.out.println(querycursor1.next());
          }
     }

     
     public static void delete() throws Exception{

          //First: instantiate the mongo object, connect the mongodb server & NBSP; Contains all databases

          //The default constructor, the default is to connect to the native, the port number, the default is 27017
          //The equivalent of Mongo Mongo =new Mongo("localhost",27017)
          Mongo mongo =new Mongo();

          //Second: connect to a specific database
          //Where the parameter is the name of the specific database, if the server does not exist, will be automatically created
          DB db=mongo.getDB("myMongo");

          //Third: operate on specific tables
         //There is no concept of tables in mongodb, but collections
          //Where the parameter is a table in the database, if it does not exist, will be automatically created
          DBCollection collection=db.getCollection("user");
          BasicDBObject queryObject1=new BasicDBObject();
          queryObject1.put("id", 2);
          queryObject1.put("name"," The little red ");

          //Delete a record
         collection.remove(queryObject1);
          //Delete all
          //collection.drop();

          DBCursor cursor1=collection.find();
          System.out.println(" The result after deletion is as follows: ");
          while(cursor1.hasNext()){
               System.out.println(cursor1.next());
          }

    
     }

    
}


Related articles: