In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to integrate MongoDB and Java in development", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve the integration of MongoDB and Java in development" article.
1. What is MongoDB?
MongoDB is a very popular NoSQL open source database. It is suitable for collections rather than tables and documents rather than rows and columns, providing high performance, high availability, and easy scalability.
Sample file:
{"_ id": ObjectId ("5b0d226b31a5f6595a7034de"), "firstName": "Dharam", "lastName": "Rajput"} 1.1 what do we need
MongoDB 3.6
MongoDB-Java-Driver 2.10.1
JDK 1.8
Maven 3.0.3
1.2 required dependency org.mongodbmongo-java-driver2.10.1
Now let's start implementing Mongo queries with Java, starting with the CRUD operation.
two。 Connection to MongoClient
If we use a version of MongoDB earlier than 2.10.0, then we use a MongoDB server, but we use a later version, so we will use MongoClient to establish a connection to MongoDB.
MongoClient mongo = new MongoClient ("localhost", 27017); / / If we use older version thanMongo mongo = new Mongo ("localhost", 27017); 3. Connection to the database
Now connect to the database. If your database does not exist, Mongo will create a new database.
DB database = mongoClient.getDB ("testdb")
If we are using Mongo in safe mode, authentication is required.
MongoClient mongoClient = new MongoClient (); DB database = mongoClient.getDB ("testdb"); / / testdb is db nameboolean auth = database.authenticate ("username", "password" .toCharray ())
Use the following code to check which database already exists.
MongoClient.getDatabaseNames (). ForEach (System.out::println); 4. Mongo collection
Now, create a collection equivalent to the tables in RDBMS. We can set the collection to:
Database.createCollection ("users", null)
Gets and prints all existing collections of the selected database.
Database.getCollectionNames () forEach (System.out::println); 5. Insert document
Now we will save a document (data) in the collection (table).
DBCollection table = db.getCollection ("users"); BasicDBObject document = new BasicDBObject (); document.put ("firstName", "Dharam"); document.put ("lastName", "Rajput"); table.insert (document)
A document has now been inserted into the database.
{"_ id": ObjectId ("5b0d226b31a5f6595a7034de"), "firstName": "Dharam", "lastName": "Rajput"} 6. Update document
Suppose we have the following documents:
{"_ id": ObjectId ("5b0d226b31a5f6595a7034de"), "firstName": "Dharam", "lastName": "Rajput"}
We want to change the name of this document.
First search the document for name= "Dharam" and update it with the new value "Dharmendra"
BasicDBObject query = new BasicDBObject (); query.put ("firstName", "Dharam"); BasicDBObject newDocument = new BasicDBObject (); newDocument.put ("firstName", "Dharmendra"); BasicDBObject updateObj = new BasicDBObject (); updateObj.put ("$set", newDocument); 7. Find documents in the collection
Search the user collection for the document "firstName = Dharmendra"
DBCollection db= db.getCollection ("user"); BasicDBObject searchQuery = new BasicDBObject (); searchQuery.put ("firstName", "Dharmendra"); DBCursor cursor = db.find (searchQuery); while (cursor.hasNext ()) {System.out.println (cursor.next ());} 8. Delete a file
Delete the document "firstName = Dharmendra".
DBCollection db= db.getCollection ("user"); BasicDBObject searchQuery = new BasicDBObject (); searchQuery.put ("name", "mkyong"); db.remove (searchQuery)
This tutorial is a quick introduction to MongoDB and Java.
Now find the complete code for MongoDB and Java integration here.
Package com.demo.mongodb;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.MongoClient;public class TestDB {public static void main (String [] args) {try {/ * Connect to MongoDB * / Since 2.10.0, uses MongoClientMongoClient mongoClient = new MongoClient ("localhost", 27017) / * Get database * / if database doesn't exists, MongoDB will create it for youDB db = mongoClient.getDB ("testdb"); mongoClient.getDatabaseNames () .forEach (System.out::println); / * Get collection / table from 'testdb' * / / if collection doesn't exists, MongoDB will create it for youDBCollection collection = db.getCollection ("users"); / * Insert * * / create a document to store key and valueBasicDBObject document = new BasicDBObject () Document.put ("firstName", "Dharam"); document.put ("lastName", "Rajput"); collection.insert (document); / * Find and display * / BasicDBObject searchQuery = new BasicDBObject (); searchQuery.put ("firstName", "Dharam"); DBCursor dbCursor = collection.find (searchQuery); while (dbCursor.hasNext ()) {System.out.println (dbCursor.next ()) } / * Update * * / / search document where name= "Dharam" and update it with new values "Dharmendra" BasicDBObject dbQuery = new BasicDBObject (); dbQuery.put ("firstName", "Dharam"); BasicDBObject newDocument = new BasicDBObject (); newDocument.put ("firstName", "Dharmendra"); BasicDBObject updateObj = new BasicDBObject (); updateObj.put ("$set", newDocument); collection.update (dbQuery, updateObj) / * Find and display * / BasicDBObject findQuery = new BasicDBObject (). Append ("firstName", "Dharmendra"); DBCursor findCursor = collection.find (findQuery); while (findCursor.hasNext ()) {System.out.println (findCursor.next ());}} catch (Exception e) {e.printStackTrace () } the above is the content of this article on "how to achieve the integration of MongoDB and Java in development". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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.
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.