البرمجة

إضافة بيانات إلى MongoDB باستخدام Java

To retrieve a specific category from your shop and insert goods into it using Java and MongoDB, you can follow these steps:

  1. Connect to your MongoDB database.

  2. Retrieve the collection containing your shop’s data.

  3. Query the document containing your shop’s data and extract the desired category.

  4. Update the category by adding goods to it.

  5. Save the updated document back to the database.

Here’s an example code snippet to illustrate this process:

java
import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class MongoDBExample { public static void main(String[] args) { // Connect to MongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase database = mongoClient.getDatabase("your_database_name"); // Get the collection MongoCollection collection = database.getCollection("shop"); // Query the document containing your shop's data Document query = new Document("_id", new ObjectId("5757df25612c2445af329111")); Document shopDocument = collection.find(query).first(); // Get the category you want to update Document mobilePhonesCategory = (Document) shopDocument.get("shop.category.MobilePhones"); // Add a new goods item to the category Document newGoods = new Document("title", "iPhone 13").append("price", 999).append("status", "available"); ((List) mobilePhonesCategory.get("goodsList")).add(newGoods); // Save the updated document back to the database collection.replaceOne(query, shopDocument); // Close the connection mongoClient.close(); } }

This code assumes that you have a MongoDB server running on your local machine, and you have a database named “your_database_name” containing a collection named “shop”. Adjust the connection details and collection names according to your setup.

المزيد من المعلومات

لإضافة المزيد من المعلومات، يُمكننا تعديل الكود السابق ليتضمن الآتي:

  1. استخدام الكلاس Category و Goods لتمثيل الفئات والبضائع.
  2. تحويل البيانات المسترجعة من قاعدة البيانات إلى كائنات من الكلاسات المعنية.
  3. إضافة بضاعة جديدة إلى الفئة المراد تعديلها.
  4. حفظ التغييرات في قاعدة البيانات.

إليك كيف يمكن تعديل الكود بما يتوافق مع هذه الخطوات:

java
import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.types.ObjectId; import java.util.List; public class MongoDBExample { public static void main(String[] args) { // Connect to MongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase database = mongoClient.getDatabase("your_database_name"); // Get the collection MongoCollection collection = database.getCollection("shop"); // Query the document containing your shop's data Document query = new Document("_id", new ObjectId("5757df25612c2445af329111")); Document shopDocument = collection.find(query).first(); // Get the category you want to update List categories = (List) shopDocument.get("shop.category"); Document mobilePhonesCategory = categories.stream() .filter(category -> category.containsKey("MobilePhones")) .findFirst() .orElse(null); if (mobilePhonesCategory != null) { // Convert the category document to a Category object Category mobilePhones = convertDocumentToCategory(mobilePhonesCategory); // Add a new goods item to the category Goods newGoods = new Goods("iPhone 13", 999, "available"); mobilePhones.getGoodsList().add(newGoods); // Convert the Category object back to a document Document updatedMobilePhonesCategory = convertCategoryToDocument(mobilePhones); // Update the category in the shopDocument categories.remove(mobilePhonesCategory); categories.add(updatedMobilePhonesCategory); // Save the updated document back to the database collection.replaceOne(query, shopDocument); } // Close the connection mongoClient.close(); } private static Category convertDocumentToCategory(Document document) { Category category = new Category(); category.setName(document.keySet().iterator().next()); category.setGoodsList((List) document.get(category.getName())); return category; } private static Document convertCategoryToDocument(Category category) { Document document = new Document(); document.append(category.getName(), category.getGoodsList()); return document; } }

يرجى ملاحظة أنه يجب تعديل الكود ليناسب هيكل البيانات الذي تستخدمه في قاعدة البيانات الخاصة بك، ويفترض أيضًا أن تكون لديك فهم جيد لكيفية تحويل البيانات بين كائنات الكلاس ومستندات MongoDB.

زر الذهاب إلى الأعلى