• NoSql实验(四)MongoDB数据库操作


    Student文档如下:

    {

    “name”: “zhangsan”,

    “score”: {

    “English”: 69,

    “Math”: 86,

    “Computer”: 77

    }

    }

    {

    “name”: “lisi”,

    “score”: {

    “English”: 55,

    “Math”: 100,

    “Computer”: 88

    }

    }

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

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

     

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

     

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

     

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

     

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

    (1)添加数据:English:45        Math:89 Computer:100

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

    {

    “name”: “scofield”,

    “score”: {

    “English”: 45,

    “Math”: 89,

    “Computer”: 100

    }

    }

      

    MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
    DB db = mongoClient.getDB("test");
    DBCollection collection = db.getCollection("student");
    DBObject doc = new BasicDBObject();
    doc.put("name", "scofield");
    Map<String,String> map=new HashMap<String, String>();
    map.put("english","45");
    map.put("math","89");
    map.put("computer","100");
    doc.put("score",map);
    db.getCollection("student").insert(doc);

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

     

    MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
    DB db = mongoClient.getDB("test");
    DBCollection collection = db.getCollection("student");
    DBObject doc = new BasicDBObject();
    doc.put("name","scofield");
    DBCursor cursor = collection.find(doc);
    try{
         while(cursor.hasNext()){
             System.out.println("student集合所拥有的name--[" + cursor.next().get("name") + "]");
         }
     }finally{
         cursor.close();
     }
     System.out.println("student集合中的记录数为----------->" + cursor.count());
     System.out.println("student集合数据格式化后的JSON串为-->" + JSON.serialize(cursor));
  • 相关阅读:
    智慧城市顶层设计策略方案(PPT)
    ant build.xml 解释!
    Excel poi API基础教程!
    操纵Excel文件的 ExcelUtil 类 !
    在selenium测试中使用XPATH功能函数starts-with、contains、descendant、ancestor、text()定位网页元素
    [ Selenium2 从零开始 by Bruce from http://seleniumcn.cn ] 1-8 视频集锦
    selenium 概念及练习 !
    selenium Object Page 设计模式理解及实现!
    使用TestNG 和 CSV文件进行数据驱动
    如何让评审人爱上我
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15511520.html
Copyright © 2020-2023  润新知