• MongoDb 操作数据库


    1,连接数据库:

    cmd 进入终端,输入 mongo

    输入 show dbs 可以看到所以的数据库:

     刚开始会有这三个数据库

    2,创建数据库(加入创建一个 hello 的数据库)

    需要 往 hello 数据库的 user 集合中插入一条数据,数据库才真正创建成功

    3,插入(增加)数据

    db.user.insert ( { " name " : " jack " })

    for 循环可以添加多条数据:

    4,显示当前的数据集合:

     5,查询所有记录:

    db.user.find( ) 

    6,查询 age=18的记录:

    7,查询 age>18 的记录:

    8,查询 age<18 的记录:

    9,查询 age>=18的记录:

    10,查询age<=18的记录

     11,查询 age >=18 小于23的记录:

     12,查询 name 中包含 8 的数据:

     13,查询 name 中以 Jack 开头的:

    > db.user.find({"name":/^jack/})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a0"), "ID" : 1, "name" : "jack1", "age" : 6 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a1"), "ID" : 2, "name" : "jack2", "age" : 7 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a2"), "ID" : 3, "name" : "jack3", "age" : 8 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a3"), "ID" : 4, "name" : "jack4", "age" : 9 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a4"), "ID" : 5, "name" : "jack5", "age" : 10 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a5"), "ID" : 6, "name" : "jack6", "age" : 11 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a6"), "ID" : 7, "name" : "jack7", "age" : 12 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a7"), "ID" : 8, "name" : "jack8", "age" : 13 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a8"), "ID" : 9, "name" : "jack9", "age" : 14 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a9"), "ID" : 10, "name" : "jack10", "age" : 15 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2aa"), "ID" : 11, "name" : "jack11", "age" : 16 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "ID" : 12, "name" : "jack12", "age" : 17 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 18 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "ID" : 14, "name" : "jack14", "age" : 19 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "ID" : 15, "name" : "jack15", "age" : 20 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2af"), "ID" : 16, "name" : "jack16", "age" : 21 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b0"), "ID" : 17, "name" : "jack17", "age" : 22 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b1"), "ID" : 18, "name" : "jack18", "age" : 23 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b2"), "ID" : 19, "name" : "jack19", "age" : 24 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b3"), "ID" : 20, "name" : "jack20", "age" : 25 }
    >                                 
    

    14,查询指定列(name)数据

    > db.user.find({},{"name":1})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a0"), "name" : "jack1" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a1"), "name" : "jack2" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a2"), "name" : "jack3" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a3"), "name" : "jack4" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a4"), "name" : "jack5" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a5"), "name" : "jack6" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a6"), "name" : "jack7" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a7"), "name" : "jack8" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a8"), "name" : "jack9" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a9"), "name" : "jack10" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2aa"), "name" : "jack11" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "name" : "jack12" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "name" : "jack13" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "name" : "jack14" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "name" : "jack15" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2af"), "name" : "jack16" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b0"), "name" : "jack17" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b1"), "name" : "jack18" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b2"), "name" : "jack19" }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b3"), "name" : "jack20" }
    >                    
    

    15,查询指定列(naem,age>18)的数据:

    > db.user.find({"age":{$gt:18}},{"name":1,"age":1})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "name" : "jack14", "age" : 19 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "name" : "jack15", "age" : 20 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2af"), "name" : "jack16", "age" : 21 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b0"), "name" : "jack17", "age" : 22 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b1"), "name" : "jack18", "age" : 23 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b2"), "name" : "jack19", "age" : 24 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b3"), "name" : "jack20", "age" : 25 }
    >                 
    

    16,按照年龄排序(1 升序,-1 降序)

    > db.user.find().sort({"age":1})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a0"), "ID" : 1, "name" : "jack1", "age" : 6 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a1"), "ID" : 2, "name" : "jack2", "age" : 7 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a2"), "ID" : 3, "name" : "jack3", "age" : 8 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a3"), "ID" : 4, "name" : "jack4", "age" : 9 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a4"), "ID" : 5, "name" : "jack5", "age" : 10 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a5"), "ID" : 6, "name" : "jack6", "age" : 11 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a6"), "ID" : 7, "name" : "jack7", "age" : 12 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a7"), "ID" : 8, "name" : "jack8", "age" : 13 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a8"), "ID" : 9, "name" : "jack9", "age" : 14 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a9"), "ID" : 10, "name" : "jack10", "age" : 15 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2aa"), "ID" : 11, "name" : "jack11", "age" : 16 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "ID" : 12, "name" : "jack12", "age" : 17 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 18 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "ID" : 14, "name" : "jack14", "age" : 19 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "ID" : 15, "name" : "jack15", "age" : 20 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2af"), "ID" : 16, "name" : "jack16", "age" : 21 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b0"), "ID" : 17, "name" : "jack17", "age" : 22 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b1"), "ID" : 18, "name" : "jack18", "age" : 23 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b2"), "ID" : 19, "name" : "jack19", "age" : 24 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b3"), "ID" : 20, "name" : "jack20", "age" : 25 }
    >                            
    

    降序:

    > db.user.find().sort({"age":-1})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b3"), "ID" : 20, "name" : "jack20", "age" : 25 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b2"), "ID" : 19, "name" : "jack19", "age" : 24 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b1"), "ID" : 18, "name" : "jack18", "age" : 23 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b0"), "ID" : 17, "name" : "jack17", "age" : 22 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2af"), "ID" : 16, "name" : "jack16", "age" : 21 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "ID" : 15, "name" : "jack15", "age" : 20 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "ID" : 14, "name" : "jack14", "age" : 19 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 18 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "ID" : 12, "name" : "jack12", "age" : 17 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2aa"), "ID" : 11, "name" : "jack11", "age" : 16 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a9"), "ID" : 10, "name" : "jack10", "age" : 15 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a8"), "ID" : 9, "name" : "jack9", "age" : 14 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a7"), "ID" : 8, "name" : "jack8", "age" : 13 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a6"), "ID" : 7, "name" : "jack7", "age" : 12 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a5"), "ID" : 6, "name" : "jack6", "age" : 11 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a4"), "ID" : 5, "name" : "jack5", "age" : 10 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a3"), "ID" : 4, "name" : "jack4", "age" : 9 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a2"), "ID" : 3, "name" : "jack3", "age" : 8 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a1"), "ID" : 2, "name" : "jack2", "age" : 7 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a0"), "ID" : 1, "name" : "jack1", "age" : 6 }
    >                           
    

    17,查询前5条数据:

    > db.user.find().limit(5)
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a0"), "ID" : 1, "name" : "jack1", "age" : 6 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a1"), "ID" : 2, "name" : "jack2", "age" : 7 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a2"), "ID" : 3, "name" : "jack3", "age" : 8 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a3"), "ID" : 4, "name" : "jack4", "age" : 9 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a4"), "ID" : 5, "name" : "jack5", "age" : 10 }
    >
    

    18,查询 10 条以后的数据:

    > db.user.find().skip(10)
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2aa"), "ID" : 11, "name" : "jack11", "age" : 16 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "ID" : 12, "name" : "jack12", "age" : 17 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 18 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "ID" : 14, "name" : "jack14", "age" : 19 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "ID" : 15, "name" : "jack15", "age" : 20 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2af"), "ID" : 16, "name" : "jack16", "age" : 21 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b0"), "ID" : 17, "name" : "jack17", "age" : 22 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b1"), "ID" : 18, "name" : "jack18", "age" : 23 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b2"), "ID" : 19, "name" : "jack19", "age" : 24 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2b3"), "ID" : 20, "name" : "jack20", "age" : 25 }
    >
    

    19,查询第6条到第15条数据(共10条):

    > db.user.find().limit(10).skip(5)
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a5"), "ID" : 6, "name" : "jack6", "age" : 11 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a6"), "ID" : 7, "name" : "jack7", "age" : 12 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a7"), "ID" : 8, "name" : "jack8", "age" : 13 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a8"), "ID" : 9, "name" : "jack9", "age" : 14 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a9"), "ID" : 10, "name" : "jack10", "age" : 15 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2aa"), "ID" : 11, "name" : "jack11", "age" : 16 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "ID" : 12, "name" : "jack12", "age" : 17 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 18 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ad"), "ID" : 14, "name" : "jack14", "age" : 19 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "ID" : 15, "name" : "jack15", "age" : 20 }
    >
    

    20,or 查询:

    > db.user.find({$or:[{age:18},{age:20}]})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 18 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ae"), "ID" : 15, "name" : "jack15", "age" : 20 }
    >
    

    21,findOne 查询第一条数据:

    > db.user.findOne()
    {
            "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a0"),
            "ID" : 1,
            "name" : "jack1",
            "age" : 6
    }
    >
    

    22,查询某个结果集的记录条数,统计数量:

    > db.user.find({age:{$gte:20}}).count()
    6
    > db.user.find({age:{$lte:20}}).count()
    15
    >
    

    如果要返回限制之后的记录数量,要使用 count( true ) 或者 count(非0)

    > db.user.find().skip(10).limit(5).count(true)
    5
    >
    

    23,修改数据:查找名字叫做 jack13 的,把其年龄改为60

    > db.user.update({"name":"jack13"},{$set:{"age":60}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.user.find({"name":jack13})
    2020-06-18T20:58:09.541+0800 E QUERY    [js] ReferenceError: jack13 is not defined :
    @(shell):1:15
    > db.user.find({"name":"jack13"})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "ID" : 13, "name" : "jack13", "age" : 60 }
    >

    没有写 $set ,会执行替换操作:

    > db.user.update({"name":"jack13"},{"age":60})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.user.find({"name":"jack13"})
    > db.user.find({"ID":13})
    > db.user.find({"age":60})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ac"), "age" : 60 }
    >
    

    24,把所有名字以 2结尾 的,都改为 tom:

    > db.user.update({"name":/2$/},{$set:{"name":"tom"}},{multi:true})
    WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
    > db.user.find({"name":/2$/})
    > db.user.find({"name":"tom"})
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2a1"), "ID" : 2, "name" : "tom", "age" : 7 }
    { "_id" : ObjectId("5eeb59f89b5a0b09e5f8c2ab"), "ID" : 12, "name" : "tom", "age" : 17 }
    >
    

    25,删除当前所在的数据库:

    db.dropDatabase( )

    26,删除数据:

    > db.user.insert({"ID":21,"name":"jack21",age:26})
    WriteResult({ "nInserted" : 1 })
    > db.user.find("name":"jack21")
    2020-06-18T21:12:04.405+0800 E QUERY    [js] SyntaxError: missing ) after argument list @(shell):1:19
    > db.user.find({"name":"jack21"})
    { "_id" : ObjectId("5eeb680e9b5a0b09e5f8c2b4"), "ID" : 21, "name" : "jack21", "age" : 26 }
    > db.user.remove({"name":"jack21"})
    WriteResult({ "nRemoved" : 1 })
    > db.user.find({"name":"jack21"})
    >
    

      

  • 相关阅读:
    VS2013连接SQLSERVER数据库时显示无法添加数据连接
    线段树模板
    网格中的极大子矩形的另类解法
    斜率优化
    三维前缀和
    Math Magic ZOJ
    01背包 多重背包 完全背包模板记录
    多重背包的单调队列优化
    Largest Rectangle in a Histogram POJ
    Game with string CodeForces
  • 原文地址:https://www.cnblogs.com/shanlu0000/p/13160044.html
Copyright © 2020-2023  润新知