栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java操纵MongoDB练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java操纵MongoDB练习

要求:用java操作mongodb

  1. 新建两个集合(college和student),信息如下,要求:同时插入多个文档(insertMany)

//学院(college)信息

{
  "_id" : ObjectId("5941f2bac1bc86928f4de49a"),
  "part" : "图书馆",
  "loc" : "南望山"
}
{
  "_id" : ObjectId("5941f2bac1bc86928f4de49b"),
  "part" : "计算机学院",
  "loc" : "未来城"
}
{
  "_id" : ObjectId("5941f2bac1bc86928f4de49c"),
  "part" : "珠宝学院",
  "loc" : "南望山"
},
{
  "_id" : ObjectId("5941f2bac1bc86928f4de49d"),
  "part" : "经管学院",
  "loc" : "未来城"
}

//学生(student)信息

{
  "_id" : ObjectId("5941f5bfc1bc86928f4de4ac"),
  "sname" : "林冲",
  "gender" : "男",

"age" : "18",
  "tel" : "13810001000",
  "major":"计算机学院",
  "location":{
        "province":"湖北",
        "city":"武汉"
  }
}
{
  "_id" : ObjectId("5941f5bfc1bc86928f4de4ad"),
  "sname" : "孙二娘",
  "gender" : "女",

"age" : "19",
  "tel" : "13812221222",
  "major":"珠宝学院",
  "location":{
        "province":"河南",
        "city":"郑州"
  }
}
{
  "_id" : ObjectId("5941f5bfc1bc86928f4de4ae"),
  "sname" : "扈三娘",
  "gender" : "女",

"age" : "20",
  "tel" : "13813331333",
  "major":"经管学院",
  "location":{
        "province":"湖南",
        "city":"长沙"
  }
}

2.向student集合插入如下文档

{
  "_id" : ObjectId("5941f5bfc1bc86928f4ae4de"),
  "sname" : "扈四娘",
  "gender" : "男",

"age" : "20",
  "tel" : "13814441444",
  "major":"经管学院",
}

3.查询student集合中的文档,并统计文档数量

4.查询sname为扈四娘的文档,添加一个location属性

  "location":{
        "province":"四川",
        "city":"重庆"
  }

5.使用{sname:"李白"} 替换 sname 为 扈四娘 的文档

6.查询age>=19的学生信息

7.查询每个学生所在校区(未来城、南望山)

import java.util.*;
import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
public class test {
    public static void main(String[]arg)
    {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        //创建数据库对象,连接MongoDB服务器,端口号为27017
        MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
        //连接数据库
        System.out.println("连接成功!");

        //第一题
        System.out.println("第一题:");
        MongoCollection collection01 = mongoDatabase.getCollection("college");
        //新建集合
       collection01.drop();
        //清空集合
        Document doc01=new Document("_id","5941f2bac1bc86928f4de49a").append("part", "图书馆").append("loc","南望山");
        Document doc02=new Document("_id","5941f2bac1bc86928f4de49b").append("part","计算机学院").append("loc","未来城");
        Document doc03=new Document("_id","5941f2bac1bc86928f4de49c").append("part", "珠宝学院").append("loc","南望山");
        Document doc04=new Document("_id","5941f2bac1bc86928f4de49d").append("part", "经管学院").append("loc", "未来城");
        //定义文档
        ArrayList documents = new ArrayList<>();
        documents.add(new Document("_id","5941f2bac1bc86928f4de49a").append("part", "图书馆").append("loc","南望山"));
        documents.add(doc02);
        documents.add(doc03);
        documents.add(doc04);
        collection01.insertMany(documents);
        System.out.println("college文档插入成功");
        //插入多条数据,定义一个document列表,然后用add()方法添加元素,最后用insertMany()方法插入

        MongoCollection collection02 = mongoDatabase.getCollection("student");
        collection02.drop();
        Document location01=new Document("province","湖北").append("city", "武汉");
        Document location02=new Document("province","湖南").append("city", "长沙");
        Document location03=new Document("province","河南").append("city", "郑州");
        Document doc05=new Document("_id","5941f5bfc1bc86928f4de4ac").append("sname", "林冲").append("gender","男").append("age", "18").append("tel", "13810001000").append("major", "计算机学院").append("location",location01);
        Document doc06=new Document("_id","5941f5bfc1bc86928f4de4ad").append("sname", "孙二娘").append("gender","女").append("age", "19").append("tel", "13812221222").append("major", "珠宝学院").append("location",location03);
        Document doc07=new Document("_id","5941f5bfc1bc86928f4de4ae").append("sname", "扈三娘").append("gender","女").append("age", "20").append("tel", "13813331333").append("major", "经管学院").append("location",location02);
        ArrayList documents1 = new ArrayList<>();
        documents1.add(doc05);
        documents1.add(doc06);
        documents1.add(doc07);
        collection02.insertMany(documents1);
        System.out.println("student文档插入成功");
        System.out.println();

        //第二题
        System.out.println("第二题:");
	       collection02.deleteOne(Filters.eq("sname","扈四娘"));
	        Document doc08=new Document("_id","5941f5bfc1bc86928f4ae4de").append("sname", "扈四娘").append("gender","男").append("age","20").append("tel","13814441444").append("major", "经管学院");
	        collection02.insertOne(doc08);
	        System.out.println("文档插入成功");

	        MongoCursor mongoCursor02 = collection02.find().iterator();
            //获取游标
	       while (mongoCursor02.hasNext()) {
               //通过游标遍历检索出的文档集合
	               System.out.println(mongoCursor02.next());
	            }
           System.out.println();


        //第三题
        System.out.println("第三题:");
         System.out.println("student集合中共有"+collection02.count()+"个文档。");//获取集合中有几个文档
        System.out.println();

        //第四题
        

        //第五题
        System.out.println("第五题:");
	      collection02.deleteOne(Filters.eq("sname","李白"));
	       Document doc11=new Document("sname","李白");
	       collection02.replaceOne(Filters.eq("sname","扈四娘"), doc11);
           //将过滤器找到的文档替换成新文档

	        MongoCursor mongoCursor04 = collection02.find().iterator();//获取游标
	       while (mongoCursor04.hasNext()) {
               //通过游标遍历检索出的文档集合
	               System.out.println(mongoCursor04.next());
                   //输出更换后的集合
	            }
        System.out.println();


        //第六题
        System.out.println("第六题:");
	        MongoCursor mongoCursor05 = collection02.find(Filters.gte("age","19")).iterator();
            //gte表示大于等于
        	        while (mongoCursor05.hasNext()) {
                        //通过游标遍历检索出的文档集合
	               System.out.println(mongoCursor05.next());
	            }
        System.out.println();


        //第七题
        System.out.println("第七题:");
       FindIterable result = collection01.find();
        for(Document doc12:result) //将查询后的结果存入result容器中
        {
            String part=doc12.getString("part"); //将part属性赋值给变量
            String loc=doc12.getString("loc");
            collection02.updateOne(Filters.eq("major",part), new Document("$set",new Document("loc",loc)));
            //先进行过滤条件,找到major属性与part相同的文档,然后在该文档后面追加loc属性
        }

        MongoCursor mongoCursor06 = collection02.find().iterator();//获取游标
        while (mongoCursor06.hasNext()) {
            //通过游标遍历检索出的文档集合
            System.out.println(mongoCursor06.next());
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873714.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号