zoukankan      html  css  js  c++  java
  • Mongo数据库实验

    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集合;

    use test01

    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'},{'score':1})

     

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

    db.Student.update({name:'lisi'},{$set:{'score.Math':95}})

    db.Student.find({name:'lisi'})

         

     

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

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

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

    {

    “name”: “scofield”,

    “score”: {

    “English”  : 45,

    “Math”: 89,

    “Computer”: 100

    }

    }

      

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

     

     

     

     

     

    import java.util.ArrayList;
    import java.util.List;
     
    import org.bson.Document;
    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoCursor;
    import com.mongodb.client.MongoDatabase;
    
    public class TestMongoDB {
    
        public static void main(String[] args) {
            MongoCollection<Document> collection = getCollection("test01","Student");
            insert(collection);
            find(collection);
        }
        
        public static MongoCollection<Document> getCollection(String dbname,String collectionname){
            MongoClient  mongoClient=new MongoClient("localhost",27017);
            MongoDatabase mongoDatabase = mongoClient.getDatabase(dbname);
            MongoCollection<Document> collection = mongoDatabase.getCollection(collectionname);
            System.out.println("连接成功");
            return collection;
        }
        public static void insert(MongoCollection<Document> collection){
            Document document=new Document("name","scofield").append("score", new Document("English",45).append("Math", 89).append("Computer",100));
            List<Document> documents=new ArrayList<Document>();
            documents.add(document);
            collection.insertMany(documents);
            System.out.println("插入成功!");
        }
        public static void find(MongoCollection<Document> collection){
            try{
                MongoCursor<Document>  cursor= collection.find(new Document("name","scofield")).projection(new Document("score",1)).iterator();
                while(cursor.hasNext()){
                    System.out.println(cursor.next().toJson());
                }
            }catch(Exception e){
                System.out.println("查找失败");
                System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            }
        }
    }

     

  • 相关阅读:
    redhat 6.4下PXE+Kickstart无人值守安装操作系统
    ubuntu14.04安装好Hadoo之后接着安装hbase和介绍常用命令
    避坑之Hadoop安装伪分布式(Hadoop3.2.0/Ubuntu14.04 64位)
    kindeditor文件上传设置文件说明为上传文件名(JSP版)
    sqlmap 扫描注入漏洞
    局域网内访问不同网段的主机(转记)
    cmd创建用户开启3389命令
    用python来更改windows开机密码
    代码安全之上传文件
    web渗透(转)
  • 原文地址:https://www.cnblogs.com/a8047/p/15652599.html
Copyright © 2011-2022 走看看