//获取连接对象 这个是资源需要释放
MongoClient mc=new MongoClient("localhost",27017);
//释放资源
mc.close();
废弃方法 --获取所有数据库的名字
List tableNamse=mc.getDatabaseNames();
for (String dnames : tableNamse) {
System.out.println(dnames);
}
迭代器遍历
MongoIterable dnames = mc.listDatabaseNames();
MongoCursor iterator = dnames.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
增强for循环遍历
for (String dnamelist : dnames) {
System.out.println(dnamelist);
}
MongoDatabase db = mc.getDatabase("shujuku");
通过建立的连接对象 mc 的getDatebase方法,寻找使用名为"javaee2309"的库,如果该库不存在则创建该库(名为"javaee2309"的数据库)
通过获取的数据库对象 db 来获取集合
MongoCollection col = db.getCollection("person");
获取集合,如果该集合不存在则创建该集合,
注意:若该集合不存在,想要创建集合,且不在此集合中插入数据,则该集合为虚拟集合,不会真正地创建,所以要真实地创建该集合,需要在后续对该集合进行插入数据的操作。
通过获取集合创建的 col对象来进行增加操作
col.insertOne(new Document("name","李四"));
Document doc1 = new Document();
doc1.append("name", "张三");
doc1.append("age", 88);
doc1.append("sex", "男");
doc1.append("bir", "1999-1-1");
doc1.append("score", 100.1);
Document doc2 = new Document();
doc2.append("name", "李四");
doc2.append("age", 30);
doc2.append("sex", "女");
doc2.append("bir", new Date());
doc2.append("score", 10.8);
Document doc3 = new Document();
doc3.append("name", "王五");
doc3.append("age", 16);
doc3.append("sex", "女");
doc3.append("bir", new Date());
doc3.append("score", 80.5);
因为插入多条数据入参需要集合类型,所以将这些数据放入集合
List doclist = new ArrayList();
doclist.add(doc1);
doclist.add(doc2);
doclist.add(doc3);
插入多条数据
col.insertMany(doclist);
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
//db.student.update({'name':'老六'},{ 'age':20});
// Bson eq=Filters.eq("name", "老六");
//组织条件
Bson gt = Filters.gt("age", 30);
Document doc =new Document();
// doc.append("$set", new Document("age",20));
doc.append("$set", new Document("age",18));
UpdateResult updateMany = col.updateMany(gt, doc);
// UpdateResult updateOne = col.updateOne(eq, doc);
// System.out.println(updateOne);
System.out.println(updateMany);
mc.close();
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
DeleteResult deleteOne = col.deleteOne(new Document("name","张三"));
// DeleteResult deleteMany = col.deleteMany(new Document("age","{$lte:20}"));
System.out.println(deleteOne);
mc.close();
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
FindIterable find = col.find();
for (Document document : find) {
System.out.println(document);
}
mc.close();
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
Document doc =new Document();
FindIterable find =
col.find(Filters.and(Filters.eq("sex",false),Filters.lt("age", 50)));
for (Document document : find) {
System.out.println(document);
}
mc.close();
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
//模糊查询声明是一个正则表达式,此处不需要加 / /
// Bson bson = Filters.regex("name", "帅$");
// FindIterable find = col.find(bson);
// for (Document doc : find ) {
// System.out.println(doc);
// }
FindIterable limit = col.find().skip(1).limit(3);
for (Document document : limit) {
System.out.println(document);
}
mc.close();
//排序的实现
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
FindIterable find = col.find().sort(new Document("age", -1));
for (Document doc : find) {
System.out.println(doc);
}
mc.close();
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
//模糊查询声明是一个正则表达式,此处不需要加 / /
Bson bson = Filters.regex("name", "^王");
FindIterable find = col.find(bson);
for (Document doc : find ) {
System.out.println(doc);
}
mc.close();
MongoClient mc=new MongoClient("localhost",27017);
MongoDatabase db = mc.getDatabase("myschool");
MongoCollection col = db.getCollection("student");
//模糊查询声明是一个正则表达式,此处不需要加 / /
Bson bson = Filters.regex("name", "帅$");
FindIterable find = col.find(bson);
for (Document doc : find ) {
System.out.println(doc);
}
mc.close();