MongoDB Learning (2) Visual Interface and mongodb Visualization

Source: Internet
Author: User

MongoDB Learning (2) Visual Interface and mongodb Visualization
1. Installation visual interface 1. mongobooster installation and configuration 1. Download and install

: Https://nosqlbooster.com/downloads

After the download is complete, click Install:

After the installation is complete, the following page is displayed:

2. Click the create link:

3. Click the Test Connection Test link:

4. Test OK and click Save & Connect to link to the local database:

2. write operation statements in Booster

1. view the set

2. Add a new data entry.

3. modify a data record

4. view documents

2. Simple Application in Java 1. Prepare the driver package

2. Obtain the names of all databases
1 package com. mongodb. study. test; 2 3 import com. mongodb. mongo; 4 import org. junit. test; 5 6 import java. util. list; 7 8/** 9 * @ author zt1994 2018/3/9 */11 public class TestMongoDB {12 13/** 14 * get the names of all databases 15 */16 @ Test17 public void testGetAllDBName () {18 // 1. create a mongo Object 19 Mongo mongo = new Mongo ("localhost", 27017); 20 List <String> databaseNames = mongo. getDatabaseNames (); 21 for (String databaseName: databaseNames) {22 System. out. println ("databaseName:" + databaseName); 23} 24} 25}

Console output:

3. Obtain all sets in the specified database
1/** 2 * Get all sets under the specified database 3 */4 @ Test 5 public void testGetCollections () {6 // 1. create a mongo object 7 Mongo mongo = new Mongo ("localhost", 27017); 8 // 2. link to the specified database 9 DB db = mongo. getDB ("zt01"); 10 Set <String> collectionNames = db. getCollectionNames (); 11 for (String collectionName: collectionNames) {12 System. out. println ("collectionName:" + collectionName); 13} 14}

Console output:

4. Get the specified set in the specified database and obtain data
1/** 2 * Get the specified set in the specified database and get data 3 */4 @ Test 5 public void testGetOneCollection () {6 // 1. create a mongo object 7 Mongo mongo = new Mongo ("localhost", 27017); 8 // 2. link to the specified database 9 DB db = mongo. getDB ("zt01"); 10 DBCollection collection = db. getCollection ("zt01"); 11 // get data 12 DBCursor dbObjects = collection. find (); 13 for (DBObject dbObject: dbObjects) {14 System. out. println ("dbObject:" + dbObject. toString (); 15} 16}

Console output:

5. add data 1. Create a tool library and connect to the specified database set
1 package com. mongodb. study. util; 2 3 import com. mongodb. DB; 4 import com. mongodb. DBCollection; 5 import com. mongodb. mongo; 6 7/** 8 * @ author zt1994 2018/3/9 9 */10 public class MongoDBUtil {11 12/** 13 * create a link to the specified database set 14 * @ return15 */16 public static DBCollection createCollection () {17 // 1. create mongo object 18 Mongo mongo = new Mongo ("localhost", 27017); 19 // 2. link to the specified database 20 DB db = mongo. getDB ("zt01"); 21 // 3. create a collection 22 DBCollection collection = db. getCollection ("zt01"); 23 return collection; 24} 25}
2. Add data to the test
1/** 2 * Test to add data 3 */4 @ Test 5 public void testAdd () {6 DBCollection collection = MongoDBUtil. createCollection (); 7 // 1. data Object 8 BasicDBObject basicDBObject = new BasicDBObject (); 9 // 2. add data 10 basicDBObject. append ("id", "01"); 11 basicDBObject. append ("name", "new data"); 12 // 3. insert to 13 collections in the collection. insert (basicDBObject); 14 // 4. view the collection 15 DBCursor dbObjects = collection. find (); 16 for (DBObject dbObject: dbObjects) {17 System. out. println ("dbObject:" + dbObject. toString (); 18} 19}

Console output:

6. delete data
1/** 2 * delete data 3 */4 @ Test 5 public void testDelete () {6 DBCollection collection = MongoDBUtil. createCollection (); 7 // 1. data Object 8 BasicDBObject basicDBObject = new BasicDBObject (); 9 // 2. specify the data to be deleted 10 basicDBObject. append ("id", "01"); 11 // 3. delete 12 collections of data. remove (basicDBObject); 13 // 4. view the collection 14 DBCursor dbObjects = collection. find (); 15 for (DBObject dbObject: dbObjects) {16 System. out. println ("dbObject:" + dbObject. toString (); 17} 18}

Console output:

You can see that the data with "id": "01" has been deleted.

7. Update and modify data
1/** 2 * update data 3 */4 @ Test 5 public void testUpdate () {6 DBCollection collection = MongoDBUtil. createCollection (); 7 // 1. data Object 8 BasicDBObject basicDBObject = new BasicDBObject (); 9 // 2. specify the data 10 basicDBObject to be modified. append ("name", "mike"); 11 // 3. specify the modified data 12 BasicDBObject updateObj = new BasicDBObject (); 13 updateObj. append ("nameUpdate", "test name modification"); 14 // 4. update Data 15 collection. update (basicDBObject, updateObj); 16 // 5. view the collection 17 DBCursor dbObjects = collection. find (); 18 for (DBObject dbObject: dbObjects) {19 System. out. println ("dbObject:" + dbObject. toString (); 20} 21}

Console output:

We can see that the data has been modified.

8. View specified data
1/** 2 * query specified data 3 */4 @ Test 5 public void testQuery () {6 DBCollection collection = MongoDBUtil. createCollection (); 7 // 1. data Object 8 BasicDBObject basicDBObject = new BasicDBObject (); 9 // 2. the field 10 basicDBObject contained in the data to be queried. append ("title", "mongodb"); 11 // 3. query data 12 DBCursor dbObjects = collection. find (basicDBObject); 13 for (DBObject dbObject: dbObjects) {14 System. out. println ("dbObject:" + dbObject. toString (); 15} 16}

Console output:

 

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.