redis、mongdb数据库操作

 

目录

Redis数据库操作。

MongoDB数据库操作


Redis数据库操作。

Student键值对如下:

zhangsan:{

English: 69

Math: 86

Computer: 77

lisi:{

English: 55

Math: 100

Computer: 88

根据上面给出的键值对,完成如下操作:

  1. 用Redis的哈希结构设计出学生表Student(键值可以用student.zhangsan和student.lisi来表示两个键值属于同一个表);

2.用hgetall命令分别输出zhangsan和lisi的成绩信息;

3.用hget命令查询zhangsan的Computer成绩;

4.修改lisi的Math成绩,改为95。

MongoDB数据库操作

Student文档如下:

{

“name”: “zhangsan”,

“score”: {

“English”: 69,

“Math”: 86,

“Computer”: 77

}

}

{

“name”: “lisi”,

“score”: {

“English”: 55,

“Math”: 100,

“Computer”: 88

}

}

1.根据上面给出的文档,完成如下操作:(20分)

(1)用MongoDB Shell设计出student集合;

(2)

用find()方法输出两个学生的信息;

(3)用find()方法查询zhangsan的所有成绩(只显示score列);

(4)修改lisi的Math成绩,改为95。

2.根据上面已经设计出的Student集合,用MongoDB的Java客户端编程,实现如下操作:

(1)添加数据:English为45 ,Math为89,Computer为100。(10分)

    与上述数据对应的文档形式如下:

{

“name”: “scofield”,

“score”: {

“English”: 45,

“Math”: 89,

“Computer”: 100

}

}

修改后的代码:

public static void insert(){

        try{

            //连接MongoDB,指定连接数据库名,指定连接表名。

            MongoCollection<Document> collection= getCollection("ku","student");    //数据库名:School 集合名:student

            //实例化一个文档,文档内容为{sname:'Mary',sage:25},如果还有其他字段,可以继续追加append
      
           Document doc1=new Document("English","45").append("Math", 89).append("Computer",100);//s=({sname:'mary',sage:25}),有几个键值对,加几个append

            //实例化一个文档,文档内容为{sname:'Bob',sage:20},集合没有顺序

           Document doc2=new Document("name","scofield").append("score", doc1);

            List<Document> documents = new ArrayList<Document>();

            //将doc1、doc2加入到documents列表中

            //documents.add(doc1);

            documents.add(doc2);

            //将documents插入集合

            collection.insertMany(documents); 

            System.out.println("插入成功");

        }catch(Exception e){

            System.err.println( e.getClass().getName() + ": " + e.getMessage() );

        }

    }

public static void insert(){

        try{

            //连接MongoDB,指定连接数据库名,指定连接表名。

            MongoCollection<Document> collection= getCollection("ku","student");    //数据库名:School 集合名:student

            //实例化一个文档,文档内容为{sname:'Mary',sage:25},如果还有其他字段,可以继续追加append

           

           Document doc1=new Document("English","45").append("Math", 89).append("Computer",100);//s=({sname:'mary',sage:25}),有几个键值对,加几个append

            //实例化一个文档,文档内容为{sname:'Bob',sage:20},集合没有顺序

           Document doc2=new Document("name","scofield").append("score", doc1);

            List<Document> documents = new ArrayList<Document>();

            //将doc1、doc2加入到documents列表中

            //documents.add(doc1);

            documents.add(doc2);

            //将documents插入集合

            collection.insertMany(documents); 

            System.out.println("插入成功");

        }catch(Exception e){

            System.err.println( e.getClass().getName() + ": " + e.getMessage() );

        }

    }

(2)获取scofield的所有成绩信息(只显示score列)。(10分)

代码:

   public static void find(){

        try{

            MongoCollection<Document> collection = getCollection("ku","student");  //数据库名:School 集合名:student

            //通过游标遍历检索出的文档集合
           MongoCursor<Document>  cursor= collection.find(new Document("name","scofield")). projection(new Document("score",1).append("_id", 0)).iterator();   //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)

            //查询所有数据
           // MongoCursor<Document>  cursor= collection.find().iterator();
            while(cursor.hasNext()){

                System.out.println(cursor.next().toJson());

            }

        }catch(Exception e){

            System.err.println( e.getClass().getName() + ": " + e.getMessage() );

        }

    }

    public static void find(){

        try{

            MongoCollection<Document> collection = getCollection("ku","student");  //数据库名:School 集合名:student

            //通过游标遍历检索出的文档集合

           MongoCursor<Document>  cursor= collection.find(new Document("name","scofield")). projection(new Document("score",1).append("_id", 0)).iterator();   //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)

            //查询所有数据

           // MongoCursor<Document>  cursor= collection.find().iterator();

            while(cursor.hasNext()){

                System.out.println(cursor.next().toJson());

            }

        }catch(Exception e){

            System.err.println( e.getClass().getName() + ": " + e.getMessage() );

        }

    }

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值