BAEL-5374: push Into Array in MongoDB (#11916)

This commit is contained in:
Kapil Khandelwal 2022-03-11 00:20:08 +05:30 committed by GitHub
parent f2b3b8e5fe
commit fee01d60fb
2 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,99 @@
package com.baeldung.mongo;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
public class PushOperations {
private static MongoClient mongoClient;
private static String testCollectionName;
private static String databaseName;
public static void setUp() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
}
databaseName = "baeldung";
testCollectionName = "orders";
}
public static void pushOperationUsingDBObject() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
DBObject listItem = new BasicDBObject("items", new BasicDBObject("itemName", "PIZZA MANIA").append("quantity", 1)
.append("price", 800));
BasicDBObject searchFilter = new BasicDBObject("customerId", 1023);
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$push", listItem);
UpdateResult updateResult = collection.updateOne(searchFilter, updateQuery);
System.out.println("updateResult:- " + updateResult);
}
public static void pushOperationUsingDocument() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.push("items", item));
System.out.println("updateResult:- " + updateResult);
}
public static void addToSetOperation() {
MongoDatabase database = mongoClient.getDatabase(databaseName);
MongoCollection<Document> collection = database.getCollection(testCollectionName);
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.addToSet("items", item));
System.out.println("updateResult:- " + updateResult);
}
public static void main(String args[]) {
//
// Connect to cluster (default is localhost:27017)
//
setUp();
//
// Push document into the array using DBObject
//
pushOperationUsingDBObject();
//
// Push document into the array using Document.
//
pushOperationUsingDocument();
//
// Push document into the array using addToSet operator.
//
addToSetOperation();
}
}

View File

@ -0,0 +1,92 @@
package com.baeldung.mongo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import org.bson.Document;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.UpdateResult;
public class PushOperationLiveTest {
private static MongoClient mongoClient;
private static MongoDatabase db;
private static MongoCollection<Document> collection;
@BeforeClass
public static void setup() {
if (mongoClient == null) {
mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDatabase("baeldung");
collection = db.getCollection("orders");
collection.insertOne(
Document.parse("{\n" + " \"customerId\": 1023,\n" + " \"orderTimestamp\": NumberLong(\"1646460073000\"),\n" + " \"shippingDestination\": \"336, Street No.1 Pawai Mumbai\",\n" + " \"purchaseOrder\": 1000,\n"
+ " \"contactNumber\":\"9898987676\",\n" + " \"items\": [ \n" + " {\n" + " \"itemName\": \"BERGER\",\n" + " \"quantity\": 1,\n" + " \"price\": 500\n" + " },\n"
+ " {\n" + " \"itemName\": \"VEG PIZZA\",\n" + " \"quantity\": 1,\n" + " \"price\": 800\n" + " } \n" + " ]\n" + " }"));
}
}
@Test
public void givenOrderCollection_whenPushOperationUsingDBObject_thenCheckingForDocument() {
DBObject listItem = new BasicDBObject("items", new BasicDBObject("itemName", "PIZZA MANIA").append("quantity", 1)
.append("price", 800));
BasicDBObject searchFilter = new BasicDBObject("customerId", 1023);
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$push", listItem);
UpdateResult updateResult = collection.updateOne(searchFilter, updateQuery);
Document orderDetail = collection.find(Filters.eq("customerId", 1023))
.first();
assertNotNull(orderDetail);
assertFalse(orderDetail.isEmpty());
}
@Test
public void givenOrderCollection_whenPushOperationUsingDocument_thenCheckingForDocument() {
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.push("items", item));
Document orderDetail = collection.find(Filters.eq("customerId", 1023))
.first();
assertNotNull(orderDetail);
assertFalse(orderDetail.isEmpty());
}
@Test
public void givenOrderCollection_whenAddToSetOperation_thenCheckingForDocument() {
Document item = new Document().append("itemName", "PIZZA MANIA")
.append("quantity", 1)
.append("price", 800);
UpdateResult updateResult = collection.updateOne(Filters.eq("customerId", 1023), Updates.addToSet("items", item));
Document orderDetail = collection.find(Filters.eq("customerId", 1023))
.first();
assertNotNull(orderDetail);
assertFalse(orderDetail.isEmpty());
}
@AfterClass
public static void cleanUp() {
mongoClient.close();
}
}