MongoDB Quick Start Note (eight) MongoDB Java driver Operation code explanation _MARIADB

Source: Internet
Author: User
Tags mongoclient mongodb name database

MongoDB Java driver is thread-safe, for general applications, as long as a MONGO instance, MONGO has a built-in connection pool (pool size defaults to 10).

The following code gives you an introduction to the Java-driven operations of MongoDB, as shown in the following code:

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.regex.Pattern;
Import org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.MongoCredential;
Import com.mongodb.ServerAddress;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.client.MongoDatabase; public class Mongozyh {public static void main (string[] args) {try {//Connect to MongoDB Service, serveraddress () Two parameters are server address and port S
Erveraddress serveraddress = new ServerAddress ("localhost", 27017);
list<serveraddress> Addrs = new arraylist<serveraddress> ();
Addrs.add (serveraddress); Three parameters are user name database name password mongocredential credential = mongocredential. Createscramsha1credential ("Zyh", "admin", "zyh". ToC
Hararray ());
List<mongocredential> credentials = new arraylist<mongocredential> ();
Credentials.add (credential);
Obtain MongoDB connection Mongoclient via connection authentication mongoclient = new Mongoclient (Addrs, credentials); Connecting to DataLibrary Mongodatabase mongodatabase = mongoclient.getdatabase ("Zyhdb");
Creates a new collection and creates a new empty set//Mongodatabase.createcollection ("student") in the database after execution;
System.out.println ("new collection succeeded");
Gets the collection and inserts data into the collection mongocollection<document> mongocollection = mongodatabase. GetCollection ("student");
Insert a piece of data//Document document = new document ();
Document.append ("name", "Zhangsan");
Document.append ("Age", 28);
Mongocollection.insertone (document);
System.out.println ("Insert a piece of data successfully");
Insert multiple data//list<document> documentlist = new arraylist<document> ();
Document Document1 = new document ();
Document1.append ("name", "Lisi");
Document1.append ("Age", 28);
Document1.append ("Sex", "male");
Document Document2 = new document ();
Document2.append ("name", "Wangwu");
Document2.append ("Age", 31);
Document2.append ("Sex", "male");
Documentlist.add (Document1);
Documentlist.add (Document2);
Mongocollection.insertmany (documentlist);
System.out.println ("Insert multiple data successfully"); Query data//queryAll data in the collection//finditerable<document> finditerable = Mongocollection.find ();
mongocursor<document> mongocursor = Finditerable.iterator (); while (Mongocursor.hasnext ()) {//System.out.println (Mongocursor.next ());//}//query based on criteria//Document query = new Docum
ENT ();
Query.put ("Age", New Document ("$lt", 30));
Query.put ("Sex", "male");
Query.put ("name", query);
Regular expression query//pattern = Pattern.compile ("^zhang");
Query.put ("name", pattern);
Sort//Document sort = new Document (); Sort.put ("Name",-1);
1 is a positive sequence,-1 is reverse//finditerable<document> finditerable = mongocollection.find (query)//. Sort (sort);
mongocursor<document> mongocursor = Finditerable.iterator (); while (Mongocursor.hasnext ()) {//Document doc = Mongocursor.next ();//System.out.print ("Name:" + doc.get ("name") + "
...");
System.out.print ("Age:" + doc.get ("age") + "...");
System.out.println ("Sex:" + doc.get ("sex") + "..."); }//Mongocollection.findoneandupdate (query conditions, modifiedcapacity); Query the first data and modify//mongocollection.findoneanddelete (query conditions); Query out the first data and delete//mongocollection.findoneandreplace (query condition, replace content);
Query the first piece of data and replace//modify data//Document query = new document ();
Query.put ("Age", 28);
Document update = new document ();
Document d = new document ();
D.put ("Birthday", New Date ());
D.put ("name", "Zhangsan");
Update.put ("$set", D); Mongocollection.updateone (query, update);
Modify the first Data//mongocollection.updatemany (query condition, modify content) of the query;//Modify all data//delete data/Document query = new document ();
Query.put ("Age", 28); Mongocollection.deleteone (query); Deletes the first Data//mongocollection.deletemany (query condition) to the query; Delete all data//Mongocollection.drop () from the query;
Delete Collection} catch (Exception e) {e.printstacktrace ();}} }

About small make up to introduce the MongoDB Quick Start Note (eight) of the MongoDB Java driver code to explain so much, I hope to help you, but also to continue to update MongoDB relevant knowledge, please pay attention to cloud Habitat community website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.