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

How to encapsulate an operation class in MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to encapsulate an operation class in MongoDB. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

MongoDB operation class encapsulation

The mongobase code is as follows:

Package com.fun.mongodb; import com.fun.frame.SourceCode;import com.mongodb.MongoClient;import com.mongodb.MongoClientURI;import com.mongodb.MongoCredential;import com.mongodb.ServerAddress;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoDatabase;import org.bson.Document; import java.util.Arrays;import java.util.List / * * basic class of mongo operation class * / public class MongoBase extends SourceCode {/ * obtain the service address list * * @ param addresses * @ return * / public static List getServers (ServerAddress... Addresses) {return Arrays.asList (addresses);} / * * obtain service address * * @ param host * @ param port * @ return * / public static ServerAddress getServerAdress (String host, int port) {return new ServerAddress (host, port);} / * * obtain authentication list * * @ param credentials * @ return * / public static List getCredentials (MongoCredential... Credentials) {return Arrays.asList (credentials);} / * * obtain verification * * @ param userName * @ param database * @ param password * @ return * / public static MongoCredential getMongoCredential (String userName, String database, String password) {return MongoCredential.createCredential (userName, database, password.toCharArray ()) } / * get mongo client * * @ param addresses * @ param credentials * @ return * / public static MongoClient getMongoClient (List addresses, List credentials) {return new MongoClient (addresses, credentials);} / * Connect to mongo database * * @ param mongoClient * @ param databaseName * @ return * / public static MongoDatabase getMongoDatabase (MongoClient mongoClient, String databaseName) {return mongoClient.getDatabase (databaseName) } / * Connect mongo set * * @ param mongoDatabase * @ param collectionName * @ return * / public static MongoCollection getMongoCollection (MongoDatabase mongoDatabase, String collectionName) {return mongoDatabase.getCollection (collectionName);} / * close database connection * * @ param mongoClient * / public static void MongoOver (MongoClient mongoClient) {mongoClient.close () } / * get mongo client object, create * * @ param mongoObject * @ return * / public static MongoClient getMongoClient (MongoObject mongoObject) {MongoClient mongoClient = new MongoClient (getServers (getServerAdress (mongoObject.host, mongoObject.port)), getCredentials (getMongoCredential (mongoObject.user, mongoObject.database, mongoObject.password)) through servers and credentials objects; return mongoClient } / * get the mongo client object, and connect * * @ param mongoObject * @ return * / public static MongoClient getMongoClientOnline (MongoObject mongoObject) {String format = String.format ("mongodb://%s:%s@%s:%d/%s", mongoObject.user, mongoObject.password, mongoObject.host, mongoObject.port, mongoObject.database) through uri; return new MongoClient (new MongoClientURI (format)) } / * get collection object * * @ param mongoObject * @ return * / public static MongoCollection getCollection (MongoObject mongoObject, String collectionName) {return getMongoClient (mongoObject) .getDatabase (mongoObject.database) .getCollection (collectionName);} / * * get collection object * * @ param mongoObject * @ return * / public static MongoCollection getCollectionOnline (MongoObject mongoObject, String collectionName) {return getMongoClientOnline (mongoObject) .getDatabase (mongoObject.database) .getCollection (collectionName);}}

The code for mongoobject is as follows:

Package com.fun.mongodb; import com.mongodb.MongoClient;import com.mongodb.client.MongoCollection;import org.bson.Document; / * mongo database configuration object, for single data service, single authentication * / public class MongoObject extends MongoBase {String host; int port; String user; String password; String database; MongoClient mongoClient / * create data connection * * @ param host * @ param port * @ param user * @ param password * @ param database * / public MongoObject (String host, int port, String user, String password, String database) {this.host = host; this.port = port; this.user = user; this.password = password; this.database = database; this.mongoClient = getMongoClient (this) } / * create a database connection * * @ param port * @ param host * @ param user * @ param password * @ param database * / public MongoObject (int port, String host, String user, String password, String database) {this.host = host; this.port = port; this.user = user; this.password = database; this.mongoClient = getMongoClientOnline (this) } / * get colletion object * * @ param collectionName * @ return * / public MongoCollection getMongoCollection (String collectionName) {MongoClient mongoClientOnline = getMongoClientOnline (this); return mongoClientOnline.getDatabase (database) .getCollection (collectionName);} / * * close connection * / public void over () {MongoOver (this.mongoClient) } @ Override public MongoObject clone () {return new MongoObject (this.host, this.port, this.user, this.password, this.database);} public MongoObject clone2 () {return new MongoObject (this.port, this.host, this.user, this.password, this.database);}

The specific effect is very good. The test code is as follows:

Public static void main (String [] args) {MongoObject ready = new MongoObject ("*", 5117, "fission_record", "fission_record", "fission_record"); MongoCollection app = ready.getMongoCollection ("app_logs_20181109"); Document first = app.find (). First (); output (first); ready.over ();}

The print content is as follows:

Document {{_ id=5be4ce052ce01b21b6c26a64, _ class=com.fission.next.record.bean.AppRecordBean, user_id=5482, action_type= {"gameId": 2, "userId": "5482"}, action_extern=DataSta_Game_Starts, client_version=15, client_ip=114.5.146.239, client_imei=UNKNOWN, client_dev=xiaomi-Redmi 5 Plus, client_type=200, server_time=1541721601655, os_name=200, os_version=15, client_time=1541721577025}}

The above is how to encapsulate an operation class in MongoDB. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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