大数据——MongoDB数据库操作实例

大数据——四种数据库(MySQL,HBase,MongoDB,Redis)操作实例

问题描述:

student文档如下:

1. 根据上面给出的文档信息,用MongoDB模式设计student集合。

     a) 设计完后,用find指令浏览集合的相关信息,给出截图。

     b) 查询zhangsan 的Computer成绩,给出截图。

     c) 修改lisi的Math成绩,改为95,给出截图。

2. 根据上面已经设计出的student集合,用MongoDB的JAVA客户端编程;

     a) 添加数据:name: scofield,English:45 Math:89 Computer:100

     b) 获取scofield的English成绩信息

问题解决:

1. 根据上面给出的文档信息,用MongoDB模式设计student集合。

设计集合代码:(复制粘贴代码时,要删除注释部分,下同)

hadoop@        -VirtualBox:~$ mongo//启动MongoDB

在>提示符下,分别键入如下语句:

> use Stu //数据库Stu被自动创建

> db.createCollection('student')//创建集合student

> show dbs//显示所有数据库

> use Stu

> var stus=[{"name":"zhangsan","scores":{"English":69,"Math":86,"Computer":77}},{"name":"lisi","score":{"English":55,"Math":100,"Computer":88}}]        //注意,这个是一整行

> db.student.insert(stus)//向集合中插入数据

对应的Linux终端运行截图:

a) 设计完后,用find指令浏览集合的相关信息,给出截图。

> db.student.find().pretty() //显示集合信息

对应的Linux终端运行截图:

b) 查询zhangsan 的Computer成绩,给出截图。

> db.student.find({"name":"zhangsan"},{"_id":0,"name":0})  //查询张三的成绩,不显示id和姓名

对应的Linux终端运行截图:

c) 修改lisi的Math成绩,改为95,给出截图。

> db.student.update({"name":"lisi"}, {"$set":{"score.Math":95}} )   //修改李四的数学成绩

对应的Linux终端运行截图:

2. 根据上面已经设计出的student集合,用MongoDB的JAVA客户端编程;

(1)按Ubuntu操作系统桌面左上角的搜索图标,搜索到Eclipse,点击打开

(2)新建一个Java Project,随便起一个工程名,点击完成

a) 添加数据:name: scofield,English:45 Math:89 Computer:100

(1)右键点击工程名,新建一个class,起名mongo_insert

(2)将如下代码粘贴至mongo_insert .java

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.MongoDatabase;

public class mongo_insert {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//实例化一个mongo客户端
		MongoClient  mongoClient=new MongoClient("localhost",27017);
		//实例化一个mongo数据库
		MongoDatabase mongoDatabase = mongoClient.getDatabase("Stu");
		//获取数据库中某个集合
		MongoCollection<Document> collection = mongoDatabase.getCollection("student");
		//实例化一个文档,内嵌一个子文档
		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("文档插入成功"); 
	}
}

(3)将JDBC驱动mongo-java-driver-3.4.2.jar导入到工程中去,操作方法:工程名(右键)--buildpath--configure build path--add external jars(在Libraries栏目下),添加mongo-java-driver-3.4.2.jar(如果没有jar包,可下载:四种数据库(MySQL,HBase,Redis,MongoDB)的Java客户端所需jar包.zip-其它文档类资源-CSDN下载),点击确定。

(4)运行此mongo_insert .java,在Linux终端通过select语句查询名为scofield的学生记录是否已经被添加?

Java运行结果:

MongoDB检验结果:scofield的学生记录已经被添加

b) 获取scofield的English成绩信息

(1)为了获取scofield的English成绩信息,请将如下代码添加至新建的mongo_query.java文件中,调试运行,给出结果截图。

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;
import com.mongodb.client.model.Filters;
import static com.mongodb.client.model.Filters.eq;
public class mongo_query {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//实例化一个mongo客户端
		MongoClient  mongoClient=new MongoClient("localhost",27017);
		//实例化一个mongo数据库
		MongoDatabase mongoDatabase = mongoClient.getDatabase("Stu");
		//获取数据库中某个集合
		MongoCollection<Document> collection = mongoDatabase.getCollection("student");
		//进行数据查找,查询条件为name=scofield, 对获取的结果集只显示score这个域
		MongoCursor<Document>  cursor=collection.find( new Document("name","scofield")).
				projection(new Document("score",1).append("_id", 0)).iterator();
		while(cursor.hasNext())
			System.out.println(cursor.next().toJson());
	}
}

Java运行结果:

补充:因为我们使用的是Ubuntu16.04,其安装mongodb的方法会与Ubuntu14.04下安装mongodb的方法有些差异,如果你想在自己的机器的Ubuntu16.04上自己动手安装mongodb,请参考

How to Install MongoDB on Ubuntu 16.04 | DigitalOcean

  • 15
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值