zoukankan      html  css  js  c++  java
  • 文档型数据库mongodb介绍1-基本操作和主从

    一。 mongodb简介

        MongoDB  是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引

       MongoDB类似于solr的数据传输格式,在mongodb中基本的概念是文档、集合、数据库

    SQL术语/概念 MongoDB术语/概念 解释/说明
    database database 数据库
    table collection 数据库表/集合
    row document 数据记录行/文档
    column field 数据字段/域
    index index 索引
    table joins   表连接,MongoDB不支持
    primary key primary key 主键,MongoDB自动将_id字段设置为主键


    二 。mongodb安装和常用操作

     1》mongodb安装

        下载安装包(机器为 centos6 ) https://www.mongodb.com/download-center?jmp=nav#community

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.9.tgz
    tar zxvf mongodb-linux-x86_64-rhel62-3.4.9.tgz
    [root@bogon mongodb-linux-x86_64-rhel62-3.4.9]# ll
    total 120
    drwxr-xr-x. 2 root root  4096 Sep 19 06:54 bin
    -rw-r--r--. 1 root root 34520 Sep 11 11:21 GNU-AGPL-3.0
    -rw-r--r--. 1 root root 16726 Sep 11 11:21 MPL-2
    -rw-r--r--. 1 root root  1359 Sep 11 11:21 README
    -rw-r--r--. 1 root root 55625 Sep 11 11:21 THIRD-PARTY-NOTICES
    
     这里下载的包是编译好的程序  所以直接将 bin目录写入PATH

    修改 ~/.bash_profile文件 export PATH前添加

    PATH=$PATH:/root/mongodb-linux-x86_64-rhel62-3.4.9/bin

    执行source ~/.bash_profile 让配置马上生效

    启动报错

    exception in initAndListen: 29 Data directory /data/db not found., terminating
    需要插件/data/db目录 默认的存储目录

    mkdir -p /data/db

    启动 mongodb

    [root@bogon mongodb-linux-x86_64-rhel62-3.4.9]# mongod
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] MongoDB starting : pid=1494 port=27017 dbpath=/data/db 64-bit host=bogon
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] db version v3.4.9
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] git version: 876ebee8c7dd0e2d992f36a848ff4dc50ee6603e
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] allocator: tcmalloc
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] modules: none
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] build environment:
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten]     distmod: rhel62
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten]     distarch: x86_64
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten]     target_arch: x86_64
    2017-09-20T05:40:40.731-0400 I CONTROL  [initandlisten] options: {}
    2017-09-20T05:40:40.734-0400 W -        [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
    2017-09-20T05:40:40.813-0400 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
    2017-09-20T05:40:40.814-0400 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
    2017-09-20T05:40:40.814-0400 I STORAGE  [initandlisten] 
    2017-09-20T05:40:40.814-0400 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2017-09-20T05:40:40.814-0400 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2017-09-20T05:40:40.814-0400 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=256M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
    2017-09-20T05:40:42.872-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:40:42.872-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2017-09-20T05:40:42.872-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2017-09-20T05:40:42.872-0400 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2017-09-20T05:40:42.872-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2017-09-20T05:40:42.874-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:40:42.951-0400 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
    使用客户端尝试连接测试(默认securecrt 输入客户端命令 无法退格 所以修改仿真界面) 点击 工具栏 - 选项 - 回话选项 点击仿真


    尝试使用mongo命令连接

    默认该命令 连接localhost和端口 27017 通过 --host 指定主机  --port指定端口

    比如mongo --host localhost --port 27017

    [root@bogon ~]# mongo
    MongoDB shell version v3.4.9
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 3.4.9
    Server has startup warnings: 
    2017-09-20T05:47:58.590-0400 I STORAGE  [initandlisten] 
    2017-09-20T05:47:58.590-0400 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2017-09-20T05:47:58.590-0400 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] 
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2017-09-20T05:47:59.273-0400 I CONTROL  [initandlisten] 
    > help
            db.help()                    help on db methods
            db.mycoll.help()             help on collection methods
            sh.help()                    sharding helpers
            rs.help()                    replica set helpers
            help admin                   administrative help
            help connect                 connecting to a db help
            help keys                    key shortcuts
            help misc                    misc things to know
            help mr                      mapreduce
    
            show dbs                     show database names
            show collections             show collections in current database
            show users                   show users in current database
            show profile                 show most recent system.profile entries with time >= 1ms
            show logs                    show the accessible logger names
            show log [name]              prints out the last segment of log in memory, 'global' is default
            use <db_name>                set current database
            db.foo.find()                list objects in collection foo
            db.foo.find( { a : 1 } )     list objects in foo where a == 1
            it                           result of the last line evaluated; use to further iterate
            DBQuery.shellBatchSize = x   set default number of items to display on shell
            exit                         quit the mongo shell
    > 


      2》mongodb常用操作
        》》数据库操作

           =》show dbs --查看所有的数据库

    > show dbs
    admin  0.000GB 系统信息 比如授权 版本等
    local  0.000GB 存储当前节点数据库的信息 不会被拷贝到集群中的其他节点中 每个节点都有自己的local
           =》use test  --进入某个数据库 这个数据库可以是没有创建的

           =》 db           --显示当前的数据库

    > use test
    switched to db test
    > db
    test
    > show dbs  --虽然可以进入数据库列表中并不存在 因为test中没有数据 有数据后才能显示
    admin  0.000GB
    local  0.000GB
           =》 > db.集合名称.insert(json数据)       --数据库中存在多个集合 集合中 插入多个jsondoc  只要存在数据 这个数据库就会自动创建 并显示

    > use test                         进入test数据库
    switched to db test
    > db.userList.insert({"name":"zs"})   test数据库的集合 userList中插入json 
    WriteResult({ "nInserted" : 1 })
    > show dbs
    admin  0.000GB
    local  0.000GB
    test   0.000GB 被创建了

           =》 > db.dropDatabase()       --删除数据库  进入哪个数据库 就删除哪个

    > use test
    switched to db test
    > db.dropDatabase()
    { "dropped" : "test", "ok" : 1 }

           =》 > show collections       --显示当前数据库下所有的集合
    > use admin
    switched to db admin
    > show collections
    system.version

        》》集合操作

       =》 > db.集合名称.insert(json数据)       --进入哪个数据库 就在哪个数据库创建集合

    > use test
    switched to db test
    > db.my.insert({"name":"zs"})
    WriteResult({ "nInserted" : 1 })
       =》 > db.集合名称.find()       --查看集合的数据

    查看admin数据库下的版本信息集合

    > use admin
    switched to db admin
    > show collections
    system.version
    > db.system.version.find()
    { "_id" : "featureCompatibilityVersion", "version" : "3.4" }
    查看local数据库下的启动日志集合
    > use local
    switched to db local
    > show collections
    startup_log
    > db.startup_log.find()
    { "_id" : "bogon-1506647800402", "hostname" : "bogon", "startTime" : ISODate("2017-09-29T01:16:40Z"), "startTimeLocal" : "Thu Sep 28 18:16:40.402", "cmdLine" : {  }, "pid" : NumberLong(2682), "buildinfo" : { "version" : "3.4.9", "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e", "modules" : [ ], "allocator" : "tcmalloc", "javascriptEngine" : "mozjs", "sysInfo" : "deprecated", "versionArray" : [ 3, 4, 9, 0 ], "openssl" : { "running" : "OpenSSL 1.0.1e-fips 11 Feb 2013", "compiled" : "OpenSSL 1.0.1e-fips 11 Feb 2013" }, "buildEnvironment" : { "distmod" : "rhel70", "distarch" : "x86_64", "cc" : "/opt/mongodbtoolchain/v2/bin/gcc: gcc (GCC) 5.4.0", "ccflags" : "-fno-omit-frame-pointer -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fstack-protector-strong -fno-builtin-memcmp", "cxx" : "/opt/mongodbtoolchain/v2/bin/g++: g++ (GCC) 5.4.0", "cxxflags" : "-Woverloaded-virtual -Wno-maybe-uninitialized -std=c++11", "linkflags" : "-pthread -Wl,-z,now -rdynamic -Wl,--fatal-warnings -fstack-protector-strong -fuse-ld=gold -Wl,--build-id -Wl,-z,noexecstack -Wl,--warn-execstack -Wl,-z,relro", "target_arch" : "x86_64", "target_os" : "linux" }, "bits" : 64, "debug" : false, "maxBsonObjectSize" : 16777216, "storageEngines" : [ "devnull", "ephemeralForTest", "mmapv1", "wiredTiger" ] } }
       =》 > db.集合名称.drop()       --删除集合
    > use test
    switched to db test
    > show collections
    my
    userList
    > db.userList.drop()
    true
    > show collections
    my
      =》 > 使用js语法循环显示集合所有的方法
      第一种是help来显示所有方法

    > db.test.help
    function () {
        var shortName = this.getName();
        print("DBCollection help");
        print("	db." + shortName + ".find().help() - show DBCursor help");
        print(
            "	db." + shortName +
            ".bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j");
        print(
            "	db." + shortName +
            ".count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS");
        print(

    第二种 使用js语法  mongodb默认支持js用法 可以定义变量 逻辑操作等

    > for(var i in db.userList){print(i)}
    _mongo
    _db
    _shortName
    _fullName
    initializeUnorderedBulkOp
    initializeOrderedBulkOp
    verify
    getName
    help
    getFullName
    getMongo
    getDB
    runCommand
    runReadCommand
    find
    findOne
    insert
    remove
    _validateUpdateDoc
    _parseUpdate
    update
    save
    createIndex
    createIndexes
    ensureIndex
    reIndex
    dropIndexes
    drop
    findAndModify
    renameCollection
    validate
    diskStorageStats
    getDiskStorageStats
    pagesInRAM
    getPagesInRAM
    getShardVersion
    _getIndexesSystemIndexes
    _getIndexesCommand
    getIndexes
    getIndices
    getIndexSpecs
    getIndexKeys
    hashAllDocs
    dropIndex
    copyTo
    getCollection
    stats
    dataSize
    storageSize
    totalIndexSize
    totalSize
    convertToCapped
    exists
    isCapped
    aggregate
    group
    groupcmd
    convertToSingleObject
    mapReduce
    toString
    tojson
    shellPrint
    getShardDistribution
    getSplitKeysForChunks
    setSlaveOk
    getSlaveOk
    getQueryOptions
    getPlanCache
    setWriteConcern
    getWriteConcern
    count
    distinct
    
    addIdIfNeeded
    bulkWrite
    insertOne
    insertMany
    deleteOne
    deleteMany
    replaceOne
    updateOne
    updateMany
    findOneAndDelete
    findOneAndReplace
    findOneAndUpdate
    explain

        》》文档操作

      =》 插入文档 db.集合.insert(文档json)

    同一个集合插入两个文档      每个文档都会自动创建一个_id的唯一标识符  

    > db.roleList.insert({"name":"admin"})
    WriteResult({ "nInserted" : 1 })
    > db.roleList.insert({"name":"dba"})
    WriteResult({ "nInserted" : 1 })
    > db.roleList.find()
    { "_id" : ObjectId("59cdb84b3fc8107a74a9010e"), "name" : "admin" }
    { "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" }

      =》 更新文档 

    db.集合.update(查询条件,更新后的文档,不存在是否插入,是否更新所有查询到的文档默认false 只更新第一条)

    > db.roleList.find()
    { "_id" : ObjectId("59cdb84b3fc8107a74a9010e"), "name" : "admin" }
    { "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" }
    > db.roleList.update({name:'admin'},{name:'admin',des:'super'})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.roleList.find()
    { "_id" : ObjectId("59cdb84b3fc8107a74a9010e"), "name" : "admin", "des" : "super" }
    { "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" }
    save方法 通过_id来查询文档直接覆盖对应的数据
    > db.roleList.find()
    { "_id" : ObjectId("59cdb84b3fc8107a74a9010e"), "name" : "admin", "des" : "super" }
    { "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" }
    { "_id" : ObjectId("59cdba583fc8107a74a90110"), "name" : "admin", "des" : "super" }
    > db.roleList.save({name:'admin',des:'super11',"_id" : ObjectId("59cdb84b3fc8107a74a9010e")})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.roleList.find()
    { "_id" : ObjectId("59cdb84b3fc8107a74a9010e"), "name" : "admin", "des" : "super11" }
    { "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" }
    { "_id" : ObjectId("59cdba583fc8107a74a90110"), "name" : "admin", "des" : "super" }

      =》 删除文档

     db.集合.remove(条件,是否只删除一条 默认为false) 删除指定条件的数据

    > db.roleList.remove({name:'admin'})
    WriteResult({ "nRemoved" : 2 })
    > db.roleList.find()
    { "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" }

      =》 查询文档

    格式化json查询显示 (pretty函数)

    > db.roleList.save({ "_id" : ObjectId("59cdb8503fc8107a74a9010f"), "name" : "dba" ,des:'hello',userList:['zs','ls']})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.roleList.find().pretty()
    {
            "_id" : ObjectId("59cdb8503fc8107a74a9010f"),
            "name" : "dba",
            "des" : "hello",
            "userList" : [
                    "zs",
                    "ls"
            ]
    }
    查询使用find(查询条件),隔开的条件就是and  可以使用 $or表示拼接的 or的条件

    > db.myRoleList.save({ "name" : "dba" ,des:'hello',userList:['zs','ls'],createtime:'2017-06-18',usedCount:10})
    > db.myRoleList.insert({ "name" : "admin" ,des:'yeah',userList:['ww','zl'],createtime:'2017-07-18',usedCount:5})
    > db.myRoleList.find({usedCount:{$gt:2},name:'dba'}) 等价于sql usedCount>2 and name='dba'
    { "_id" : ObjectId("59cdbf913fc8107a74a90111"), "name" : "dba", "des" : "hello", "userList" : [ "zs", "ls" ], "createtime" : "2017-06-18", "usedCount" : 10 }
    
    > db.myRoleList.find({usedCount:{$gt:2},$or:[{name:'dba'},{name:'admin'}]}) 等价sql userdCount>2 and (name='dba' or name='admin')
    { "_id" : ObjectId("59cdbf913fc8107a74a90111"), "name" : "dba", "des" : "hello", "userList" : [ "zs", "ls" ], "createtime" : "2017-06-18", "usedCount" : 10 }
    { "_id" : ObjectId("59cdbf983fc8107a74a90112"), "name" : "admin", "des" : "yeah", "userList" : [ "ww", "zl" ], "createtime" : "2017-07-18", "usedCount" : 5 }
    
    比较符号查看(摘自 http://www.runoob.com/mongodb/mongodb-operators-type.html)

    操作

    格式

    等于

    {<key>:<value>}

    小于

    {<key>:{$lt:<value>}}

    小于或等于

    {<key>:{$lte:<value>}}

    大于

    {<key>:{$gt:<value>}}

    大于或等于

    {<key>:{$gte:<value>}}

    不等于

    {<key>:{$ne:<value>}}

    可以指定使用数据类型$type来进行判断
    > db.myRoleList.find({usedCount:{$type:1}})
    { "_id" : ObjectId("59cdbf913fc8107a74a90111"), "name" : "dba", "des" : "hello", "userList" : [ "zs", "ls" ], "createtime" : "2017-06-18", "usedCount" : 10 }
    { "_id" : ObjectId("59cdbf983fc8107a74a90112"), "name" : "admin", "des" : "yeah", "userList" : [ "ww", "zl" ], "createtime" : "2017-07-18", "usedCount" : 5 }
    类型是使用数字来标识

    类型

    数字

    备注

    Double

    1

     

    String

    2

     

    Object

    3

     

    Array

    4

     

    Binary data

    5

     

    Undefined

    6

    已废弃。

    Object id

    7

     

    Boolean

    8

     

    Date

    9

     

    Null

    10

     

    Regular Expression

    11

     

    JavaScript

    13

     

    Symbol

    14

     

    JavaScript (with scope)

    15

     

    32-bit integer

    16

     

    Timestamp

    17

     

    64-bit integer

    18

     

    Min key

    255

    Query with -1.

    Max key

    127

     

       =》 查询文档分页

        使用skip跳过到指定位置 索引从1 开始 skip(1) 从第二条开始读取

        使用limit限制返回的条数

      比如myroleList有 21条记录  每页显示5条  显示第二页 从6条开始 拿出5条

    db.myrolelist.find().skip(5).limit(5)

    三 。mongodb主从和副本集和分片集群

     1》主从复制

       模拟环境

            主 192.168.58.147 

            从 192.168.58.145 

     主从复制是 是用于备份主的数据 主用于提供主的数据备份  一般是主负责提供修改服务 从可以提供读服务分担主的压力

     192.168.58.147 启动mongod服务

     mongod --master
     192.168.58.145 启动mongod服务

    mongod --slave --source=192.168.58.147:27017
    使用mongod --host 192.168.58.147
    添加一个集合数据

    使用mongod --host 192.168.58.145

    发现能登陆不能查询   rs.slaveOk()表示可读

    查看 147上添加的数据是否自动同步到 145上 一般ok

    副本集和分片集群 参考下一篇文章

    四。 mongodb java调用

    package mongo;
    
    import java.util.Random;
    
    import org.bson.Document;
    import org.junit.Test;
    
    import com.mongodb.MongoClient;
    import com.mongodb.client.FindIterable;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoCursor;
    import com.mongodb.client.MongoDatabase;
    import com.mongodb.client.model.Filters;
    
    public class TestMongo {
    	/**
    	 * 获取mongodb的连接
    	 * @return
    	 */
    	public static MongoClient getClient(){
    		MongoClient mc=new MongoClient("192.168.58.150", 27017);
    		return mc;
    	}
    	/**
    	 * 创建集合并添加文档和document
    	 */
    	@Test
    	public void createCollection(){
    		MongoClient mc=getClient();
    		MongoDatabase database=mc.getDatabase("mydb");
    		MongoCollection<Document> mcd=database.getCollection("userInfo");
    		for(int i=0;i<=100;i++){
    			Document doc=new Document();
    			doc.append("name", "ls"+i);
    			doc.append("sex", i%2==0?"女":"男");
    			doc.append("age", new Random().nextInt(100));
    			mcd.insertOne(doc);
    		}
    	}
    	/**
    	 * 查找集合下所有文档 具体根据条件参考删除
    	 */
    	@Test
    	public void findCollection(){
    		MongoClient mc=getClient();
    		MongoDatabase database=mc.getDatabase("mydb");
    		MongoCollection<Document> mcd=database.getCollection("userInfo");
    		FindIterable<Document> ft=mcd.find();
    		MongoCursor<Document> mr=ft.iterator();
    		while(mr.hasNext()){
    			Document doc=mr.next();
    			System.out.println(doc.get("name")+"--"+doc.get("sex"));
    		}
    	}
    	/**
    	 * 更新文档
    	 * updateOne更新找到的所有结果集中的第一条
    	 * updateMany是更新所有结果集
    	 */
    	@Test
    	public void updateCollection(){
    		MongoClient mc=getClient();
    		MongoDatabase database=mc.getDatabase("mydb");
    		MongoCollection<Document> mcd=database.getCollection("userInfo");
    		
    		Document doc=new Document();
    		doc.append("sex","男");
    		//只是找到所有sex=女  将sex改成男 其他字段不修改
    		mcd.updateMany(Filters.eq("sex", "女"),new Document("$set",doc));
    	}
    	/**
    	 * 根据条件删除
    	 */
    	@Test
    	public void deleteCollection(){
    		MongoClient mc=getClient();
    		MongoDatabase database=mc.getDatabase("mydb");
    		MongoCollection<Document> mcd=database.getCollection("userInfo");
    		//删除满足条件找到的第一条记录
    		mcd.deleteOne(Filters.eq("sex", "女"));
    		//两个条件都满足的所有记录都删除
    		mcd.deleteMany(Filters.and(Filters.eq("sex", "女"),Filters.eq("name", "ls")));
    	}
    }
    


  • 相关阅读:
    ccs元素分类 gcelaor
    webkit Safari的样式库
    “Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象
    “Zhuang.Data”轻型数据库访问框架(一)开篇介绍
    一个基于Dapper的DbContext封装
    打造比Dictionary还要快2倍以上的字查找类
    .Net core 的热插拔机制的深入探索,以及卸载问题求救指南.
    字符串类型的自动转换与识别
    最近发现的.net core中的一些bugs
    从项目经理的角度看.net的MVC中Razor语法真的很垃圾.
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331175.html
Copyright © 2011-2022 走看看