zoukankan      html  css  js  c++  java
  • Mongo实验

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

    (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

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

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

     ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** **********

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

    启动mongo服务

    show dbs;

    使用test数据库

    use test

    创建新的集合student并使用

    db.createCollection('student');

    插入数据

    db.student.insert({"name":"zhangsan","score":{"English":69,"Math":86,"Computer":77}})
    db.student.insert({"name":"lisi","score":{"English":55,"Math":100,"Computer":88}})

    通过可视化工具查看结果

     

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

    db.student.find();

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

    db.student.find({name:"zhangsan"},{name:0,_id:0});

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

    db.student.update({name:"lisi"},{$set:{score:{English:55,Math:95,Computer:88}}})

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

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

    配置maven依赖(Mongo-4.4.10)

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.11.1</version>
        </dependency>
    </dependencies>

    Java代码

    1.连接数据库

    package utils;
    
    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoDatabase;
    
    public class DBUtil {
        public static MongoDatabase getMongoDB() {
            //创建客户端
            //这里仅仅是连接到了mongoDB服务器
            MongoClient client = new MongoClient("localhost",27017);
            //dbb是数据库名字,连接到数据库
            MongoDatabase mongoDatabase = client.getDatabase("test");
            return mongoDatabase;
        }
    }

    2.插入数据

    public static void insertDoc() {
        MongoCollection<Document> coll = DBUtil.getMongoDB().getCollection("student");
        //注意:一个document只能插入一次。id限制。
        Document doc = new Document();
        doc.append("name", "scofield");
        doc.append("score", Arrays.asList("45","89","100"));
        // coll.insertOne(doc);
    
        List<Document> list = new ArrayList<>();
        list.add(doc);
        coll.insertMany(list);
        System.out.println("success");
    }

    3.主函数

    public static void main(String[] args) {
        insertDoc();
    }

    运行结果:

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

    按照指定条件查询

    public static void find(){
        MongoCollection<Document> coll = DBUtil.getMongoDB().getCollection("student");
        try{
            Document document = new Document();
            document.append("_id",0);
            document.append("name",0);
            MongoCursor<Document> cursor= coll.find(new Document("name","scofield"))
                    .projection(document)
                    .iterator();
            while(cursor.hasNext()){
                System.out.println(cursor.next().toJson());
            }
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }

    主函数

    public static void main(String[] args) {
        find();
    }

    运行截图

  • 相关阅读:
    转Vtype扩展
    Can't connect to MySQL server on 'ip' (13)
    观察者+js 模式
    (转)ASP.NET架构分析
    sql得到时间
    Js+XML 操作 (转)
    js中的math对象
    property和attribute的区别
    CSS样式定义
    linux 开启 mount
  • 原文地址:https://www.cnblogs.com/Arisf/p/15472916.html
Copyright © 2011-2022 走看看