【大数据】 MongoDB 入门 【学习笔记】

7、 MongoDB

(57条消息) Java操作MongoDB详解_靖节先生的博客-CSDN博客_java mongodb 存储数据

7.1 MongoDB shell

MongoDB 教程 | 菜鸟教程 (runoob.com)

​ MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

​ MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

如何在 Docker 容器中运行 MongoDB (baidu.com)

$ docker pull mongo:3.2.6
$ docker run -d -p 27017:27017 --name mongo mongo:3.2.6
$ docker exec -it mongo mongo

$ docker start f5fcc13a46dc

1、MongoDB 创建数据库

> use Student

2、创建集合

> db.createCollection("Student")

3、插入文档(插入数据)

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

4、更新文档

> db.Student.update({'name':'lisi'},{$set:{'name':'lisi2'}})

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

5、查询文档

> db.Student.find().pretty()
> db.Student.find({"name":"zhangsan"},{score: 1}).pretty()
> db.collection.find(query, {title: 1, by: 1}) // inclusion模式 指定返回的键,不返回其他键
> db.collection.find(query, {title: 0, by: 0}) // exclusion模式 指定不返回的键,返回其他键

6、删除集合

> db.collection.drop()

7、删除文档

> db.Student.remove({"name" : "zhangsan"})

7.2 MongoDB Java

MongoDB Java | 菜鸟教程 (runoob.com)

1、导入依赖

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.2.2</version>
</dependency>

2、连接数据库

连接数据库,你需要指定数据库名称,如果指定的数据库不存在,mongo会自动创建数据库。

连接数据库的Java代码如下:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC{
   public static void main( String args[] ){
      try{   
       // 连接到 mongodb 服务
         MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
       
        // 连接到数据库
       MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol");  
       System.out.println("Connect to database successfully");
          
        /* 编写的数据库操作 */
        
      }catch(Exception e){
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
     }
   }
}

3、创建集合

mongoDatabase.createCollection("test");

4、获取集合

MongoCollection<Document> collection = mongoDatabase.getCollection("test");

5、插入文档

MongoCollection<Document> collection = mongoDatabase.getCollection("test");
System.out.println("集合 test 选择成功");
//插入文档  
/** 
* 1. 创建文档 org.bson.Document 参数为key-value的格式 
* 2. 创建文档集合List<Document> 
* 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 
*  	插入单个文档可以用mongoCollection.insertOne(Document) 
* */
Document document = new Document("title", "MongoDB").  
    append("description", "database").  
    append("likes", 100).  
    append("by", "Fly");  
List<Document> documents = new ArrayList<Document>();  
documents.add(document);  
collection.insertMany(documents);  
System.out.println("文档插入成功");  

6、检索所有文档

//选择集合
MongoCollection<Document> collection = mongoDatabase.getCollection("Student");

/** 
         * 1. 获取迭代器FindIterable<Document> 
         * 2. 获取游标MongoCursor<Document> 
         * 3. 通过游标遍历检索出的文档集合 
         * */  
FindIterable<Document> findIterable = collection.find();  
MongoCursor<Document> mongoCursor = findIterable.iterator();  
while(mongoCursor.hasNext()){  
    System.out.println(mongoCursor.next());  
}  

7、更新文档

MongoCollection<Document> collection = mongoDatabase.getCollection("test");
System.out.println("集合 test 选择成功");

//更新文档   将文档中likes=100的文档修改为likes=200   
collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200)));  
//检索查看结果  
FindIterable<Document> findIterable = collection.find();  
MongoCursor<Document> mongoCursor = findIterable.iterator();  
while(mongoCursor.hasNext()){  
    System.out.println(mongoCursor.next());  
}  

8、删除第一个文档

MongoCollection<Document> collection = mongoDatabase.getCollection("test");
System.out.println("集合 test 选择成功");

//删除符合条件的第一个文档  
collection.deleteOne(Filters.eq("likes", 200));  
//删除所有符合条件的文档  
collection.deleteMany (Filters.eq("likes", 200));  
//检索查看结果  
FindIterable<Document> findIterable = collection.find();  
MongoCursor<Document> mongoCursor = findIterable.iterator();  
while(mongoCursor.hasNext()){  
    System.out.println(mongoCursor.next());  
}  
BasicDBObject searchCond = new BasicDBObject();
BasicDBList condList = new BasicDBList();
BasicDBObject cond;
DBCollection Conllection;
condList= linkPayReturnContent(map);
if(condList.size()>0){
	searchCond.put("$and",condList);
}

/**
*去掉查询出来的主键id
*/

BasicDBObject idDB=new BasicDBObject();
idDB.put("_id",0);
Conllection=mongoBgTemp.queryDB(“指定查询的表名”);
DBCursor db=Conllection.find(searchCond,idDB);
db.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while(db.hasNext()){
    resultMap=new HashMap();
    DBObject dbObj= db.next();
    list.add(dbObj.toMap());
}

db.close();
return list;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值