设为首页 加入收藏

TOP

mongoDB基本使用(二)(一)
2015-11-21 01:33:31 来源: 作者: 【 】 浏览:0
Tags:mongoDB 基本 使用
数据库基本操作
连接到mongoDB服务器 ./bin/mongo 127.0.0.1:12345?
?
查看当前数据库
show dbs
admin ?(empty)
local ?0.078G
?
却换数据库(如果不存在会自动创建)
use jerome
switched to db jerome
?
删除数据库
db.dropDatabase()
{ "dropped" : "jerome", "ok" : 1 }
?
删除表
?
show tables
jerome_collection
jerome_coolection
system.indexes
db.jerome_collection.drop()
true
show tables #删除了当前表了
jerome_coolection
system.indexes

?

?
?
写入
(集合数据的写入,格式为JSON)
db.jerome_collection.insert({x:1})
WriteResult({ "nInserted" : 1 })
(插入成功)

?

?
查询
show dbs
admin   (empty)
jerome  0.078GB
local   0.078GB
show collections
jerome_collection
system.indexes
db.jerome_collection.find()
{ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 }
db.jerome_collection.find({x:1})    #可以指定参数
{ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 }

?

?
(_id是全局字段,在数据库中不会重复)
?
测试:再插入一条数据_id为1
?
db.jerome_collection.insert({x:3,_id:1})
WriteResult({ "nInserted" : 1 })
db.jerome_collection.insert({x:2,_id:1})
WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 11000,
        "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: jerome.jerome_collection.$_id_  dup key: { : 1.0 }"
    }
})

?

?
插入两次一样的id会报错,id不可以重复。?
?
插入多条数据测试limit等
?
for(i=3;i<100;i++)db.jerome_collection.insert({x:i}) #可以使用js语法
WriteResult({ "nInserted" : 1 })
db.jerome_collection.find().count() #查找总条数
99
db.jerome_collection.find().skip(3).limit(2).sort({x:1}) #跳过前三条,取两条,使用x排序
{ "_id" : ObjectId("556ff5e8d7e60a53de941a74"), "x" : 4 }
{ "_id" : ObjectId("556ff5e8d7e60a53de941a75"), "x" : 5 }

?

?
?
更新
Updata至少接收两个参数,一个查找的,一个更新的数据。
?
db.jerome_collection.find({x:1})
{ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 }
db.jerome_collection.update({x:1},{x:999})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.jerome_collection.find({x:1}) #已经找不到了
db.jerome_collection.find({x:999}) 
{ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 999 }

?

?
?
部分更新操作符(set )
?
db.jerome_collection.insert({x:100,y:100,z:100})
WriteResult({ "nInserted" : 1 })
db.jerome_collection.update({z:100},{$set:{y:99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.jerome_collection.find({z:100})
{ "_id" : ObjectId("556ff84a1c99195ded71252e"), "x" : 100, "y" : 99, "z" : 100 }

?

?
?
更新不存在数据时会自动创建
?
db.jerome_collection.find({y:100})
db.jerome_collection.update({y:100},{y:999},true)
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("556ff9556db7cf8009b5edf8")
})
db.jerome_collection.find({y:999})
{ "_id" : ObjectId("556ff9556db7cf8009b5edf8"), "y" : 999 }

?

?
更新多条数据
for(i=0;i<3;i++)db.jerome_collection.insert({c:2}) #插入三条
WriteResult({ "nInserted" : 1 })
db.jerome_collection.find({c:2}) 
{ "_id" : ObjectId("556ffa011c99195ded71252f"), "c" : 2 }
{ "_id" : ObjectId("556ffa011c99195ded712530"), "c" : 2 }
{ "_id" : ObjectId("556ffa011c99195ded712531"), "c" : 2 }
db.jerome_collection.update({c:2},{c:3}) #更新
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.jerome_collection.find({c:2})
{ "_id" : ObjectId("556ffa011c99195ded712530"), "c" : 2 }
{ "_id" : ObjectId("556ffa011c99195ded712531"), "c" : 2 }
db.jerome_collection.find({c:3}) #发现只更新一条,是为了防止误操作
{ "_id" : ObjectId("556ffa011c99195ded71252f"), "c" : 3 }
db.jerome_collection.update({c:2},{$set:{c:3}},false,true) #更新多条
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
db.jerome_collection.find({c:2})
db.jerome_collection.find({c:3})
{ "_id" : ObjectId("556ffa011c99195ded71252f"), "c" : 3 }
{ "_id" : ObjectId("556ffa011c99195ded712530"), "c" : 3 }
{ "_id" : ObjectId("556ffa011c99195ded712531"), "c" : 3 }

?

?
删除
(必须要有参数)
db.jerome_collection.find({c:3})
{ "_id" : ObjectId("556ffa011c99195ded71252f"), "c" : 3 }
{ "_id" : ObjectId("556ffa011c99195ded712530"), "c" : 3 }
{ "_id" : ObjectId("556ffa011c99195ded712531"), "c" : 3 }
db.jerome_collection.remove() #不可用
2015-06-04T00:15:34.444-0700 remove needs a query at src/mongo/shell/collection.js:299
db.jerome_collection.find({c:3})
{ "_id" : ObjectId("556ffa011c99195ded71252f"), "c" : 3 }
{ "_id" : ObjectId("556ffa011c99195ded712530"), "c" : 3 }
{ "_id" : ObjectId("556ffa011c99195ded712531"), "c" : 3 }
db.jerome_collection.remove({c:3}) #删除必须要有参数
WriteResult({ "nRemoved" : 3 })
db.jerome_collection.find({c:3}) #删除成功

?

?
索引
数据较多时,使用索引速度加快。
查看集合索引情况
?
for(i=0;i<100;i++)db.jerome_collection.insert({x:i}) #添加测试数据
WriteResult({ "nInserted" : 1 })
db.jerome_collection.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "jerome.jerome_collection"
    }
]

?

?
只有一个默认索引。
?
创建索引
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mongoDB介绍、安装、搭建简单的mo.. 下一篇mybatis无效的列类型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: