Download the JAR package

Central.maven.org/maven2/org/…

Two, code implementation

package com.xbq.mongodb; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.bson.types.ObjectId; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.util.JSON; /** * @ClassName: MongoDBTest * @Description: TODO MongoDB add, delete, change and check operation, * @author XBQ * @version 1.0 * @date 2017-4-5 am 11:50:06 */ Public class MongoDBTest {private static final String HOST ="192.168.242.129";
    private static final int PORT = 27017;
    private static final String DB_NAME = "testDB"; private static Mongo mongo; private static DB db; Static {// connect to MongoDB mongo = new mongo (HOST, PORT); // Open the databasetestDB db = mongo.getDB(DB_NAME); } public static void main(String[] args) { DBCollection DBCollection = db.getCollection("testTable"); // Query all collection names of the databasefor(String name : mongo.getDatabaseNames()){ System.out.println(name); } // addOne(dbCollection); // addList(dbCollection); // addByJson(dbCollection); // deleteOne(dbCollection); // deleteByIn(dbCollection); // deleteAll(dbCollection); // updateOne(dbCollection); // updateMulti(dbCollection); // queryOne(dbCollection); // queryPage(dbCollection); // queryRange(dbCollection); queryList(dbCollection); } / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = query to = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / * * * @ the Title: QueryOne * @description: TODO queries a record whose name is @param dbCollection * @return: void
     */
    public static void queryOne(DBCollection dbCollection){
        DBObject documents = new BasicDBObject("name"."Zhang"); DBObject result = dbCollection.findOne(documents); System.out.println(result); } /** * @title: queryPage * @description: TODO paging query, skip the first 2 after 3 data * @param dbCollection * @return: void
     */
    public static void queryPage(DBCollection dbCollection){
        DBCursor cursor = dbCollection.find().skip(2).limit(3);
        while(cursor.hasNext()) { System.out.println(cursor.next()); } /** * @title: queryRange * @description: TODO range query * @param dbCollection * @return: void
     */
    public static void queryRange(DBCollection dbCollection) {
        DBObject range = new BasicDBObject();
        range.put("$gte", 50);
        range.put("$lte"52); DBObject dbObject = new BasicDBObject(); dbObject.put("age", range);
        DBCursor cursor = dbCollection.find(dbObject);
        while(cursor.hasNext()) { System.out.println(cursor.next()); }} / * *'* @title: queryList * @description: TODO: query all records * @param dbCollection * @return: void */ public static void queryList(DBCollection dbCollection) { DBCursor cursor = dbCollection.find(); DBObject dbObject = null; while(cursor.hasNext()){ dbObject = cursor.next(); System.out.println(dbObject); }} / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = increase start = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / * * * @ the Title: AddOne * @description: TODO adds a new record * @param dbCollection * @return: void */ public static void addOne(DBCollection dbCollection){ DBObject documents = new Append ("age", 45).appEnd ("sex", "male ").appEnd ("address", new BasicDBObject("postCode", 100000). Append (" street ", "888 shennan avenue"), append (" city ", "shenzhen")); dbCollection.insert(documents); } /** * @title: addList * @description: TODO batch add record, add record can use various data types * @param dbCollection * @return: void */ public static void addList(DBCollection dbCollection){ List
      
        listdbo= new ArrayList
       
        (); DBObject dbObject = new BasicDBObject(); Dbobject. put("name", "old wang "); List
        
          List = new ArrayList
         
          (); List.add (" not next door "); dbObject.put("remark", list); listdbo.add(dbObject); dbObject = new BasicDBObject(); Map
          
           > map = new HashMap
           
            >(); List
            
              hobbys = new ArrayList
             
              (); Flower hobbys. Add (" see "); Hobbys. Add (" flowers "); Map. Put (" hobby ", hobbys); dbObject.put("hobby", map); listdbo.add(dbObject); dbObject = new BasicDBObject(); Dbobject. put("name", "Lao Zhang "); dbObject.put("age", 52); Dbobject. put("job", "guard old wang "); DbObject. Put ("remark", new BasicDBObject("address", "shenzhen "). Append ("street"," 888 Shenzhen Avenue ")); listdbo.add(dbObject); dbCollection.insert(listdbo); } /** * @title: addByJson * @description: TODO json after the object is added * @param dbCollection * @return: Void */ public static void addByJson(DBCollection DBCollection){String json = "{\"name\ : \", \"age\" : 66, \ "job \" : \ "guard Lao wang \", \ "remark \" : {\ "address \" : \ "guangdong province shenzhen \", \ "street \" : \ "888 shennan avenue \"}} "; DBObject dbObject = (DBObject) JSON.parse(json); dbCollection.insert(dbObject); } / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = start modify = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / * * * @ the Title: Update * @description: TODO changes the specified record * @param dbCollection * @return: Void */ public static void updateOne(DBCollection DBCollection) {DBObject qryResult = dbCollection.findOne(new ObjectId("58e4a11c6c166304f0635958")); Qryresult. put("age", 55); DBObject olddbObject = new BasicDBObject(); olddbObject.put("_id", new ObjectId("58e4a11c6c166304f0635958")); dbCollection.update(olddbObject, qryResult); } /** * @title: updateMulti * @description: TODO: void */ public static void updateMulti(DBCollection dbCollection) { DBObject newdbObject = new BasicDBObject(); Newdbobject. put("name", "zhang SAN "); Newdbobject. put("address", "shenzhen "); Newdbobject. put("remark", "Zhang SAN is a NB Coder"); DBObject olddbObject = new BasicDBObject(); Olddbobject. put("name", "zhang SAN "); UpsertValue = new BasicDBObject("$set", newdbObject); // DBObject upsertValue = new BasicDBObject("$set", newdbObject); // Insert the new data into the database. Update (olddbObject, upsertValue, true, true); update(olddbObject, upsertValue, true, true); } / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = remove start = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / * * * @ the Title: DeleteFirst * @description: TODO deleteFirst * @param * @return: void */ public static void deleteFirst(DBCollection dbCollection){ DBObject dbObject = dbCollection.findOne(); dbCollection.remove(dbObject); } /** * @title: deleteOne * @description: TODO delete a specified record * @param dbCollection * @return: void */ public static void deleteOne(DBCollection dbCollection){ DBObject dbObject = new BasicDBObject(); dbObject.put("_id", new ObjectId("58e49c2d6c166309e0d50484")); dbCollection.remove(dbObject); } /** * @title: deleteByIn @description: TODO
             
            
           ,list
          ,list
         
        
       
      12', '34') * @param dbCollection * @return: void */ public static void deleteByIn(DBCollection dbCollection) { List
      
        list = new ArrayList
       
        (); List. The add (" old zhang "); List. The add (" wang "); List. The add (" zhang "); DBObject dbObject = new BasicDBObject("$in", list); DBObject delObject = new BasicDBObject(); delObject.put("name", dbObject); dbCollection.remove(delObject); } /** * @title: deleteAll * @description: TODO deleteAll records * @param dbCollection * @return: void */ public static void deleteAll(DBCollection dbCollection){ DBCursor cursor = dbCollection.find(); while(cursor.hasNext()){ dbCollection.remove(cursor.next()); }}}
       
      Copy the code