با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن ، این جلسه از آموزش های MongoDB میخواهیم در مورد جاوا در MongoDB بحث کنیم.زبان جاوا شبیه به ++C است اما مدل شیءگرایی آسانتری دارد و از قابلیتهای سطح پایین کمتری پشتیبانی میکند.به همین دلیل مخاطبین زیادی را به خود جذب کرده است.به طور مختصر در این جلسه ما یاد می گیریم چگونه راه اندازی درایور JDBC در MongoDB را انجام دهیم.
قبل از شروع به استفاده جاوا در MongoDB ، باید مطمئن شوید که درایور JDBC در MongoDB و جاوا را روی دستگاه خود نصب کرده اید. شما می توانید آموزش Java را برای نصب جاوا بر روی دستگاه خود بررسی کنید. اکنون، اجازه دهید نحوه راه اندازی درایور JDBC در MongoDB را بررسی کنیم .
در قدم اول شما نیاز به دانلود jar از مسیر دانلود mongo.jar را دارید. اطمینان حاصل کنید که آخرین نسخه آن را دانلود کنید.
سپس شما باید mongo.jar را به classpath اضافه کنید.
برای اتصال پایگاه داده، شما باید نام پایگاه داده را مشخص کنید، اگر پایگاه داده وجود نداشته باشد، MongoDB آن را به صورت خودکار ایجاد می کند.
در زیر قطعه کد برای اتصال به پایگاه داده آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class ConnectToDB { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); System.out.println("Credentials ::"+ credential); } } |
حالا، اجازه دهید برنامه فوق را برای ایجاد پایگاه داده myDb ساخته و اجرا کنیم، همانطور که در زیر نشان داده شده است.
1 2 | $javac ConnectToDB.java $java ConnectToDB |
اجرای، برنامه فوق به شما خروجی زیرا می دهد.
1 2 3 4 5 6 7 8 | Connected to the database successfully Credentials ::MongoCredential{ mechanism = null, userName = 'sampleUser', source = 'myDb', password = <hidden>, mechanismProperties = {} } |
برای ایجاد مجموعه، متد ()createCollection از کلاس com.mongodb.client.MongoDatabase استفاده می شود.
قطعه کد برای ایجاد یک مجموعه به شرح زیر است:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class CreatingCollection { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); //Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); //Creating a collection database.createCollection("sampleCollection"); System.out.println("Collection created successfully"); } } |
بعد از کامپایل کدهای فوق خروجی زیرا مشاهده می نمایید :
1 2 | Connected to the database successfully Collection created successfully |
برای دریافت / انتخاب یک مجموعه از پایگاه داده، متد ()getCollection از com.mongodb.client.MongoDatabase استفاده می شود.
در زیر برنامه برای دریافت / انتخاب یک مجموعه آمده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class selectingCollection { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); // Creating a collection System.out.println("Collection created successfully"); // Retieving a collection MongoCollection<Document> collection = database.getCollection("myCollection"); System.out.println("Collection myCollection selected successfully"); } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 | Connected to the database successfully Collection created successfully Collection myCollection selected successfully |
برای درج یک سند در MongoDB، متد ()insertion از کلاس com.mongodb.client.MongoCollection مورد استفاده قرار می گیرد.
در زیر قطعه کد برای درج یک سند آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class InsertingDocument { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); // Retrieving a collection MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection sampleCollection selected successfully"); Document document = new Document("title", "MongoDB") .append("id", 1) .append("description", "database") .append("likes", 100) .append("url", "http://www.tutorialspoint.com/mongodb/") .append("by", "tutorials point"); collection.insertOne(document); System.out.println("Document inserted successfully"); } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 | Connected to the database successfully Collection sampleCollection selected successfully Document inserted successfully |
برای انتخاب همه اسناد مجموعه، متد () find از کلاس com.mongodb.client.MongoCollection استفاده می شود. این روش یک مکان نما را باز می کند، بنابراین شما باید این مکان نما را تکرار کنید.
در زیر برنامه برای انتخاب تمام اسناد آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.Iterator; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class RetrievingAllDocuments { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); // Retrieving a collection MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection sampleCollection selected successfully"); // Getting the iterable object FindIterable<Document> iterDoc = collection.find(); int i = 1; // Getting the iterator Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Document{{ _id = 5967745223993a32646baab8, title = MongoDB, id = 1, description = database, likes = 100, url = http://www.tutorialspoint.com/mongodb/, by = tutorials point }} Document{{ _id = 7452239959673a32646baab8, title = RethinkDB, id = 2, description = database, likes = 200, url = http://www.tutorialspoint.com/rethinkdb/, by = tutorials point }} |
برای به روز رسانی یک سند از مجموعه، متد ()updateOne از کلاس com.mongodb.client.MongoCollection استفاده می شود.
در زیر برنامه برای انتخاب اولین سند آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Updates; import java.util.Iterator; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class UpdatingDocuments { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); // Retrieving a collection MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection myCollection selected successfully"); collection.updateOne(Filters.eq("id", 1), Updates.set("likes", 150)); System.out.println("Document update successfully..."); // Retrieving the documents after updation // Getting the iterable object FindIterable<Document> iterDoc = collection.find(); int i = 1; // Getting the iterator Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 4 5 6 7 8 9 | Document update successfully... Document {{ _id = 5967745223993a32646baab8, title = MongoDB, id = 1, description = database, likes = 150, url = http://www.tutorialspoint.com/mongodb/, by = tutorials point }} |
برای حذف یک سند از مجموعه، شما باید از متد ()deleteOne از کلاس com.mongodb.client.MongoCollection استفاده کنید.
در زیر برنامه برای حذف سند آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; import java.util.Iterator; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class DeletingDocuments { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); // Retrieving a collection MongoCollection<Document> collection = database.getCollection("sampleCollection"); System.out.println("Collection sampleCollection selected successfully"); // Deleting the documents collection.deleteOne(Filters.eq("id", 1)); System.out.println("Document deleted successfully..."); // Retrieving the documents after updation // Getting the iterable object FindIterable<Document> iterDoc = collection.find(); int i = 1; // Getting the iterator Iterator it = iterDoc.iterator(); while (it.hasNext()) { System.out.println("Inserted Document: "+i); System.out.println(it.next()); i++; } } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 | Connected to the database successfully Collection sampleCollection selected successfully Document deleted successfully... |
برای حذف یک مجموعه از پایگاه داده، شما باید از متد ()drop از کلاس com.mongodb.client.MongoCollection استفاده کنید.
در زیر برنامه برای حذف یک مجموعه آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class DropingCollection { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); // Creating a collection System.out.println("Collections created successfully"); // Retieving a collection MongoCollection<Document> collection = database.getCollection("sampleCollection"); // Dropping a Collection collection.drop(); System.out.println("Collection dropped successfully"); } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 | Connected to the database successfully Collection sampleCollection selected successfully Collection dropped successfully |
برای لیست کردن تمام مجموعه ها در پایگاه داده، شما باید از متد ()listCollectionNames از کلاس com.mongodb.client.MongoDatabase استفاده کنید.
در زیر کد برای لیست کردن تمام مجموعه های پایگاه داده آورده شده است :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; public class ListOfCollection { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); // Creating Credentials MongoCredential credential; credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray()); System.out.println("Connected to the database successfully"); // Accessing the database MongoDatabase database = mongo.getDatabase("myDb"); System.out.println("Collection created successfully"); for (String name : database.listCollectionNames()) { System.out.println(name); } } } |
بعد از کامپایل برنامه فوق خروجی زیرا مشاهده می کنید :
1 2 3 4 5 | Connected to the database successfully Collection created successfully myCollection myCollection1 myCollection5 |
باقی مانده متدهای ()save()، limit()، skip()، sort و غیره را در MongoDB همانند توضیحات بالا در آموزش های بعدی توضیح خواهیم داد.