发现一篇Java操作MongoDb不错的文章,记录一下:

https://www.cnblogs.com/sa-dan/p/6836055.html

基本功能。

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; /**
* @author szy
* @version 创建时间:2018-3-31 下午9:48:59
*
*/
public class MongodbTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MongoClient mongoClient = new MongoClient("localhost", 27017);
//查询表
MongoDatabase mgdb = mongoClient.getDatabase("test"); System.out.println("Connect to database successfully!");
System.out.println("MongoDatabase inof is : "+mgdb.getName()); //表里表里的集合
for (String name : mgdb.listCollectionNames()) {
System.out.println("test表里的集合有:"+name);
} //###########################
//获得一个集合对象
MongoCollection<Document> coll = mgdb.getCollection("myTestCollection"); //如果在集合中插入相同的_id,会报错 index: _id_ dup key: {
Document document = new Document("_id", 2002).append("title", "MongoDB Insert Demo")
.append("description","database")
.append("likes", 30)
.append("by", "yiibai point")
.append("url", "http://www.yiibai.com/mongodb/"); //往集合对插入一体爱记录
coll.insertOne(document); //再插入一条不指定id的,会自动分配一个_id
Document document2 = new Document("title", "MongoDB Insert Demo")
.append("description","database")
.append("likes", 30)
.append("by", "yiibai point")
.append("url", "http://www.yiibai.com/mongodb/");
coll.insertOne(document2); //多行插入
List<Document> documents = new ArrayList<Document>();
Document a1 = new Document("title", "2018"); Document a2 = new Document("title", "2019"); documents.add(a1);
documents.add(a2); coll.insertMany(documents);
System.out.println("Document inserted successfully"); } }

  

MongoDB学习之(三)增删查改的更多相关文章

  1. 8天学通MongoDB——第二天 细说增删查改

    原文地址:http://www.cnblogs.com/huangxincheng/archive/2012/02/19/2357846.html 看过上一篇,相信大家都会知道如何开启mongodb了 ...

  2. MongoDB之二(增删查改)

    一: Insert操作 上一篇也说过,文档是采用“K-V”格式存储的,如果大家对JSON比较熟悉的话,我相信学mongodb是手到擒来,我们知道JSON里面Value 可能是“字符串”,可能是“数组” ...

  3. node.js+express+mongoose实现用户增删查改案例

    node.js+express+mongodb对用户进行增删查改 一.用到的相关技术 使用 Node.js 的 express 框架搭建web服务 使用 express 中间件 body-parse ...

  4. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  5. MongoDB在Java下的增删查改

    我们总不能一直使用cmd对数据库操作,数据库总是要在程序中使用的.今天来说一下怎么通过Java调用MongoDB. 学习一下最基本也是最常用的增删查改语句,这是使用数据库的基础. 注意事项: 1.要打 ...

  6. [MongoDB] MongoDB增删查改

    MongoDB的三元素,数据库.集合.文档,集合就是表,文档就是行 开启MongoDB,cd切换到MongoDB的安装目录下的bin目录里,使用命令mongod 开启,参数:--dbpath 路径,把 ...

  7. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  8. MongoDB数据库(二):增删查改

    MongoDB数据库的增删查改 1.插入数据 语法: db.集合名称.insert(document) db.table_name.insert({name:'gj',gender:1}) db.ta ...

  9. MongoDB的增删查改基本操作

    MongoDB的增删查改基本操作 先决条件建库.建集合.建文档 连接mongo,如果连接不上什么连接拒绝,输入mongod命令,启动服务后 输入mongo show dbs 显示当前的所有的数据库 一 ...

  10. Entity Framework(三)---FluentAPI和增删查改

    一.FluentAPI: 1.基本配置: namespace ConsoleApp14.ModelConfig { public class PersonConfig: EntityTypeConfi ...

随机推荐

  1. Linux RAID5+备份盘测试

    RAID5磁盘阵列组技术至少需要3块盘来做,加上1块备份盘(这块硬盘设备平时是闲置状态不用工作,一旦RAID磁盘阵列组中有硬盘出现故障后则会马上自动顶替上去),总共是需要向虚拟机中模拟4块硬盘设备. ...

  2. github常见操作和常见错误及其解决办法

    一.常见操作 1. 使用git在本地创建一个项目的过程 $ makdir ~/hello-world //创建一个项目hello-world $ cd ~/hello-world //打开这个项目 $ ...

  3. HDU 2256 Problem of Precision (矩阵乘法)

    Problem of Precision Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. NSURLRequestCachePolicy 缓存策略

    1> NSURLRequestUseProtocolCachePolicy = 0, 默认的缓存策略, 如果缓存不存在,直接从服务端获取.如果缓存存在,会根据response中的Cache-Co ...

  5. 解决LLDB模式下出现message sent to deallocated instance错误

    本文在源文的基础上做整理:http://www.devdiv.com/home.php?mod=space&uid=50901&do=blog&id=50856 Xcode版本 ...

  6. ASP.NET Web API使用示例

    原文地址:https://blog.csdn.net/chinacsharper/article/details/21333311 上篇博客讲解rest服务开发时,曾经提到过asp.net mvc中的 ...

  7. 【js】正则表达式(I)

    正则表达式是由英文词语regular expression翻译过来的,就是符合某种规则的表达式.正则表达式在软件开发中应用非常广泛,例如,找出网页中的超链接,找出网页中的email地址,找出网页中的手 ...

  8. python学习笔记011——内置函数sum()

    1 描述 sum() 方法对系列进行求和计算. 2 语法 sum(iterable[, start]) iterable:可迭代对象,如列表. start:指定相加的参数,如果没有设置这个值,默认为0 ...

  9. Jmeter -----计数器(counter)

    计数器的定义 Allows the user to create a counter that can be referenced anywhere in the Thread Group. The ...

  10. PLSQL_自治事务和嵌套事物的理解和用法(案例)

    2014-06-01 Created By BaoXinjian