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 Bcollection = mongoDatabase.getCollection("Bike");
//链接到Bike集合
MongoCollection 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 ar = new ArrayList();
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 ar=(ArrayList)(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 findIterable = collection.find(document); 
MongoCursor mongoCursor = findIterable.iterator(); 
while(mongoCursor.hasNext()){ 
     Document d =mongoCursor.next();//此处的d就是各个符合条件的document
}
//获取location的X值为10的所有文档

4.获取满足数组内属性的多个Document
FindIterable findIterable = collection.find(); 
MongoCursor mongoCursor = findIterable.iterator(); 
while(mongoCursor.hasNext()){ 
Document d =mongoCursor.next();      
ArrayList y =(ArrayList) (d.get("his_Record"));
Document l = (Document)(y.get(0));
if(((l.get("status")).toString()).equals("0"))
          //你的操作
}
//得到his_Record数组第一个元素的status为0的所有Document


5.程序实例——显示库中所有损坏车辆的信息








你可能感兴趣的:(mongodb_java总结)