In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to operate the MongoDB database in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
The connection between Java and mongodb
1. Even a single mongodb
Mongo mg = new Mongo (); / / Port 127.0.0.1 is 27017Mongo mg = new Mongo (ip) by default; / / you can specify that ip port defaults to 27017Mongo mg = new Mongo (ip,port); / / you can also specify ip and port
two。 Even double mongodb
/ / ip is the host ip address, port is the port number, dataBaseName is equivalent to the database name DBAddress left = new DBAddress ("ip:port/dataBaseName"); DBAddress right = new DBAddress ("ip:port/dataBaseName"); / / if there is a problem with one mongodb, it will automatically connect to another Mongo mongo = new Mongo (left, right).
3. Even multiple mongodb
List mongoHostList = new ArrayList (); mongoHostList.add (new ServerAddress ("ip", port)); mongoHostList.add (new ServerAddress ("ip", port)); mongoHostList.add (new ServerAddress ("ip", port)); Mongo mg = new Mongo (mongoHostList); Java gets the database name of mongodb
1. Get the db (database) of mongodb
/ / dataBaseName is equivalent to the database name in a relational database. If the / / database name is not found in mongodb, there will be no error. By default, mongodb will establish this database name, which is empty. DB db = mg.getDB (dataBaseName)
Note: mongodb is case-sensitive, you must pay attention to it in the program.
Db Security Certification of 2.mongodb
/ / Security authentication java code returns true to indicate pass, false indicates failed operation db.authenticate ("userName", "password" .toCharray ()); if (db.authenticate ("admin", "123" .toCharArray ()) {System.out.println ("connection mongodb succeeded...");} else {System.out.println ("connection mongodb failed...");} Java performs crud operation on mongodb collection
1. Get the collection of db in mongodb (table)
/ / the parameter tableName is equivalent to the table name in the relational database. / / if the tableName is not in the mongodb, the tableName will be created by default, with an empty DBCollection users = db.getCollection (tableName); / / list the collection list of the library, relative to the table object Set colls = db.getCollectionNames (); for (String s: colls) {} / / get a single collection DBCollection con = db.getCollection ("users")
Collection self-growing primary key for db in 2.mongodb
Like tables in traditional relational databases, Mongodb also has the concept of primary key (_ id), which is used to uniquely identify them. When a user inserts a new record into collection, if the _ id attribute is not specified, mongodb automatically generates a value of type ObjectId and saves it as the value of _ id.
The value of _ id can be of any type, except an array. In practical applications, users are encouraged to define their own value of _ id, but to ensure that it is unique.
In traditional databases, we usually use an increasing sequence to provide the primary key, and instead of using ObjectId in Mongodb, we can get the primary key in the following ways.
Public class Tools {/ * realizes the function of mongodb primary key self-growth * @ param users * @ param tableName * @ return * / public static long getNext (DBCollection users,String tableName) {long incId = 0 users.findAndModify {DBObject ret = users.findAndModify ("_ id", tableName), null, null, false, new BasicDBObject ("$inc", new BasicDBObject ("next", 1), true, true); incId = Long.valueOf (ret.get ("next"). ToString ()) } catch (Exception e) {e.printStackTrace ();} return incId;}}
3.java inserts collection.
DB db = m.getDB ("testdb"); DBCollection con = db.getCollection ("users"); / / normal addition BasicDBObject doc = new BasicDBObject (); doc.put ("name", "MongoDB"); doc.put ("type", "database"); doc.put ("count", 1); BasicDBObject info = new BasicDBObject (); info.put ("x", 102); info.put ("y", 102); doc.put ("info", info); con.insert (doc) / / insert object / / Special add / / add multiple special data (mode free) for (int I = 1; I
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.