Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the basic operation and examples of MongoDB database

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Explain the basic operation of database and examples in detail

Don't talk too much nonsense, go directly to the code, the comments are relatively clear, please refer to it.

Sample code:

/ * insert a DB object * / public static void addDBObject (DBCollection collection,BasicDBObject object) {collection.insert (object);} / * query DBObject * / public static DBObject getDBObjectById (String value) throws UnknownHostException according to id, MongoException {dbc = getDBCollection ("company", "users"). Find (new BasicDBObject ("_ id", new ObjectId (value)); DBObject ob = null; int i = 0; while (dbc.hasNext ()) {ob = dbc.next () IDefinder;} if (I = = 1) {return ob;} else {return null;}} / * query * / public static DBObject getDBObject (String key,String value) throws UnknownHostException based on key and value values, MongoException {dbc = getDBCollection ("company", "users") .find (new BasicDBObject (key,value)); DBObject ob = null; int i = 0; while (dbc.hasNext ()) {ob = dbc.next (); iSum + } if (I = = 1) {return ob;} else {return null;}} / * get (add) all the following aggregate names (table names) * / public static Set getCollectionsNames (String DBName) throws MongoException, UnknownHostException {return getDB (DBName). GetCollectionNames () } / * traverse the collection of db objects in the aggregation (equivalent to data in a relational database) * / public static Set getDBObjects (DBCollection collection) {Set dbObjects = new HashSet (); DBCursor cursor = collection.find (); while (cursor.hasNext ()) {DBObject object = cursor.next (); dbObjects.add (object);} return dbObjects } / * get / add aggregations (equivalent to relational database tables) * / public static DBCollection getDBCollection (String DBName,String collectionName) throws UnknownHostException, MongoException {return getDB (DBName) .getCollection (collectionName);} / * acquire / add databases * / public static DB getDB (String DBName) throws UnknownHostException, MongoException {return getMongo (). GetDB (DBName) } / * Connect database * / public static Mongo getMongo () throws UnknownHostException, MongoException {Mongo mg = null; if (mg = = null) {mg = new Mongo ();} return mg;} / * close connection * / public static void destory (Mongo mg) {if (mg! = null) {mg.close (); mg = null;} System.gc () } / * get database name * / public static List getDBNames () throws MongoException, UnknownHostException {return getMongo (). GetDatabaseNames ();} / * Delete database * / public static void deleteDB (String DBName) throws MongoException, UnknownHostException {getMongo (). DropDatabase (DBName);}

If you have any questions, please leave a message or go to the community to exchange and discuss, thank you for reading, hope to help you, thank you for your support!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report