MongoDB的java操作增刪改查總結篇(內嵌文件、陣列)
Mongo資料
User:
"_id" : "11373126679",
"name" : "Wangwei",
"his_Record" : [
{
"bikeId" : "309",
"status" : 1
}
],
"location" : {
"Area" : "aaa",
"X" : "10",
"Y" : "11"
},
"balance" : "98"
}
Bike
{
"_id" : "20",
"his_Record" : [
{
"userId" : "18231714812",
"status" : "1",
"miles" : 0
}
],
"location" : {
"Area" : "www",
"X" : "5",
"Y" : "2"
}
}
操作
連結
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//開啟Mongo服務,27017是mongo的預設埠
MongoDatabase mongoDatabase = mongoClient.getDatabase("aaaa");
//連結到mongo的missbike資料庫
MongoCollection<Document> Bcollection = mongoDatabase.getCollection("Bike");
//連結到Bike集合
MongoCollection<Document> Ucollection = mongoDatabase.getCollection("User");
//連結到User集合
//所有對集合到操作都會相應的在資料庫中改變
增刪改查:
增:
1.增添普通的文件
Document document = new Document(“_id” , ”123”);
Bcollection.insertOne(document);
//添加了一個_id 為 123 的記錄
2.同時新增多個屬性的文件
Document document = new Document(“_id”, “123”).
append(“name”, “xiexie”);
Ucollection.insertOne(document);
//添加了一個_id 為123, name 為xiexie的記錄
3.新增內嵌文件
Document loc = new Document(“Area”, ”China”).
append(“X”,”10”).
append(“Y”,”10”);
Document document = new Document(“location”,loc)
Ucollection.insertOne(document);
//添加了一個鍵為location, 值的 Area為China,X為10,Y為10的記錄
4.新增陣列
ArrayList<Document> ar = new ArrayList<Document>();
Document record1 = new Document(“userId”, “123”).
append(“status”,”0”).
append(“miles”,”100”);
Document record2 = new Document(“userId”, “1234”).
append(“status”,”0”).
append(“miles”,”200”);
ar.add(record1);
ar.add(record2);
Document document = new Document(“_id”,”321”).
append(“record”, ar);
Bcollection.insertOne(document);
//添加了一個_id為321, record為ar陣列的記錄
5.程式例項——增加一個使用者
刪:
1.刪除特定的記錄
Ucollection.deleteOne(Filters.eq("_id", ));
2.刪除所有符合條件的所有記錄
Document document = new Document(“balance”, “70”);
3.程式例項——刪除一個特定ID的記錄
改:
1.修改正常記錄記錄
Document x = new Document("_id","123");
Document y = new Document("$set",new Document("balance","170"));
collection.updateOne(x,y);
//將_id為123記錄的balanc改為170
2.修改內嵌文件記錄
Document x = new Document("_id","123");
Document y = new Document("$set",new Document("location.X","12"));
collection.updateOne(x,y);
//將_id為123記錄的location下的X改為12
3.修改陣列
Document y2= new Document("_id","123");
Document d = Ucollection.find(y2).first();
ArrayList<Document> ar=(ArrayList<Document>)(d.get("his_Record"));
Document newrecord = new Document("bikeId","456").append("status", "0");
ar.add(0, newrecord);
Document y3 = new Document("his_Record",ar);
Ucollection.updateOne(y2, new Document("$set",y3));
//向id為123記錄的陣列新增新的資訊
4.程式例項——充值
查(獲取Document):
1.通過簡單鍵值獲取單個Document
Document document =collection.find(new Document("_id","123")).first();
//獲取_id為123的記錄
2.通過內嵌鍵值獲取單個Document
Document document = new Document("location.X","10");
Document document1 = collection.find(document ).first();
//獲取location的X值為10的第一個文件
3.通過鍵值獲取獲取多個Document
Document document = new Document("location.X","10");
FindIterable<Document> findIterable = collection.find(document);
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
Document d =mongoCursor.next();//此處的d就是各個符合條件的document
}
//獲取location的X值為10的所有文件
4.獲取滿足陣列內屬性的多個Document
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
Document d =mongoCursor.next();
ArrayList<Document> y =(ArrayList<Document>) (d.get("his_Record"));
Document l = (Document)(y.get(0));
if(((l.get("status")).toString()).equals("0"))
//你的操作
}
//得到his_Record陣列第一個元素的status為0的所有Document
5.程式例項——顯示庫中所有損壞車輛的資訊