实验四 NoMysql和关系数据库操作 MongoDB篇(4)


实验环境:

1、操作系统:Windows10

2、MongoDB版本:4.4.0

3、JDK版本:1.8或以上版本;

4、Java IDEidea

(四)MongoDB数据库操作

Student文档如下:

“name”: “zhangsan”,

“score”: {

“English”: 69,

“Math”: 86,

“Computer”: 77

“name”: “lisi”,

“score”: {

“English”: 55,

“Math”: 100,

“Computer”: 88

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

1MongoDB Shell设计出student集合

db.createCollection("Student")
show collections
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:true,_id:false})

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

db.Student.update({"name":"lisi"},{$set:{"score":{"English":55,"Math":95"Computer":88}}})
db.Student.find({"name":"lisi"},{score:true,_id:false})

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

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

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

“name”: “scofield”,

“score”: {

“English”: 45,

“Math”: 89,

“Computer”: 100

  import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import java.util.ArrayList;
import java.util.List;

public class text{
    public static void main( String args[] ){
        try{
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");
            MongoCollection collection = mongoDatabase.getCollection("Student");
            System.out.println("集合 test 选择成功");
            Document document = new Document("name", "scofield").
                    append("score", new Document("English",45).append("Math",89).append("Computer",100));
            List documents = new ArrayList();
            documents.add(document);
            collection.insertMany(documents);
            System.out.println("文档插入成功");
            System.out.println(collection);
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
} 

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

import com.mongodb.*;
import org.bson.conversions.Bson;

public class text{
    public static void main( String args[] ){
        try{

//            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
            System.out.println("Connect to database successfully");
            MongoCollection collection = mongoDatabase.getCollection("Student");
            System.out.println("集合 test 选择成功");
            Bson filter= Filters.eq("name","scofield");
//            DBCollection users = (DBCollection) mongoDatabase.getCollection("Student");
            FindIterable findIterable=collection.find(filter).projection(new BasicDBObject("score",1).append("_id",0));
            MongoCursor cursor=findIterable.iterator();
//            DBCursor cursor=users.find();
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}

相关