博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb部署以及数据操作
阅读量:5991 次
发布时间:2019-06-20

本文共 5910 字,大约阅读时间需要 19 分钟。

hot3.png

db.hits.insert({"url":"www.datagru.cn","pv":102});

db.hits.insert({"url":"f.datagru.cn","pv":108});
db.hits.insert({"url":"www.itpub","pv":155});
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 102 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
> db.hits.update({"url":"www.datagru.cn"},{$inc:{"pv":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
$set直接设置
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "ip" : 20 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
$unset删除键值
> db.hits.update({"url":"f.datagru.cn"},{$unset:{"ip":20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
数组修改器
db.hits.update({"url":"f.datagru.cn"},{"$set":{"users":[{"name":"hang","age":12},{"name":"huang","age":21}]}})
 
 db.hits.update({"url":"f.datagru.cn"},{"$set":{"users":[{"name":"hang","age":12},{"name":"huang","age":21}]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "users" : [ { "name" : "hang", "age" : 12 }, { "name" : "huang", "age" : 21 } ] }
为数组增加元素
db.hits.update({"url":"f.datagru.cn"},{"$push":{"users":{"name":"hang","age":12}}})
> db.hits.update({"url":"f.datagru.cn"},{"$push":{"users":{"name":"hang","age":12}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "users" : [ { "name" : "hang", "age" : 12 }, { "name" : "huang", "age" : 21 }, { "name" : "hang", "age" : 12 } ] }
定位修改
db.hits.update({"url":"f.datagru.cn"},{"$set":{"users.2.name":"liu"}})
> db.hits.update({"url":"f.datagru.cn"},{"$set":{"users.2.name":"liu"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.hits.find();
{ "_id" : ObjectId("56a20fa26efa22a91f284357"), "url" : "www.datagru.cn", "pv" : 103 }
{ "_id" : ObjectId("56a20fb16efa22a91f284359"), "url" : "www.itpub", "pv" : 155 }
{ "_id" : ObjectId("56a20faa6efa22a91f284358"), "url" : "f.datagru.cn", "pv" : 108, "users" : [ { "name" : "hang", "age" : 12 }, { "name" : "huang", "age" : 21 }, { "name" : "liu", "age" : 12 } ] }
upsert
> db.runCommand({getLastError:1})
{
    "connectionId" : 1,
    "n" : 0,
    "syncMillis" : 0,
    "writtenTo" : null,
    "err" : null,
    "ok" : 1
}
mongodb查询find
db.abc.insert([
{"a":23,"b":21,"c":34},
{"a":23,"b":32},
{"a":23,"b":24,"c":54},
{"a":23,"c":37},
{"a":31,"b":21,"c":37},
{"a":31,"b":25},
{"a":31,"c":34}
]
)
> db.abc.find()
{ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "a" : 23, "b" : 21, "c" : 34 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "a" : 23, "b" : 32 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "a" : 23, "b" : 24, "c" : 54 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435d"), "a" : 23, "c" : 37 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435e"), "a" : 31, "b" : 21, "c" : 37 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435f"), "a" : 31, "b" : 25 }
{ "_id" : ObjectId("56a21d2c6efa22a91f284360"), "a" : 31, "c" : 34 }
子键筛选
> db.abc.find({"a":23})
{ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "a" : 23, "b" : 21, "c" : 34 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "a" : 23, "b" : 32 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "a" : 23, "b" : 24, "c" : 54 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435d"), "a" : 23, "c" : 37 }
> db.abc.find({"a":23},{"b":1})
{ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "b" : 21 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "b" : 32 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "b" : 24 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435d") }
> db.abc.find({"a":23},{"b":1},{"c":1})1感兴趣
{ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "b" : 21 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "b" : 32 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "b" : 24 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435d") }
> db.abc.find({"a":23},{"b":0})
{ "_id" : ObjectId("56a21d2c6efa22a91f28435a"), "a" : 23, "c" : 34 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435b"), "a" : 23 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435c"), "a" : 23, "c" : 54 }
{ "_id" : ObjectId("56a21d2c6efa22a91f28435d"), "a" : 23, "c" : 37 }
> db.abc.find({"a":23},{"_id":0})
{ "a" : 23, "b" : 21, "c" : 34 }
{ "a" : 23, "b" : 32 }
{ "a" : 23, "b" : 24, "c" : 54 }
{ "a" : 23, "c" : 37 }
> db.abc.find({"a":23},{"_id":0,"b":1})不能零和一混合除开_id
{ "b" : 21 }
{ "b" : 32 }
{ "b" : 24 }
{  }
操作符
$lt $lte              db.abc.find({b:{"$gte":30,"$lt":35}})
$gt $gte
$all
$exists
$mod                   db.abc.find({b:{"$mod":[5,1]}})
$ne
$in                    db.abc.find({b:{"$in":[25,32]}})
$nin
$nor
$or                     db.abc.find({"$or":[{"b":32},{"c":54}]})
$size 元素个数
$slice列出一部分元素
$type

转载于:https://my.oschina.net/goudingcheng/blog/608215

你可能感兴趣的文章
SAP云平台对Kubernetes的支持
查看>>
JVM执行方法调用(一)- 重载与重写
查看>>
PAT A1043
查看>>
一些关于Rust在2019年的思考
查看>>
关于Vuex的action传入多个参数的问题
查看>>
ggit (git gui) --- 开发记录 (一)
查看>>
143. Reorder List
查看>>
JS 中的== 与 ===
查看>>
hostingranking.cn·基于ghost的轻量技术架构整理
查看>>
CountDownLatch的await和countDown方法简单分析
查看>>
【CuteJavaScript】Angular6入门项目(4.改造组件和添加HTTP服务)
查看>>
代码片段
查看>>
for of, for in, forEach比较
查看>>
Java高级特性增强-多线程
查看>>
手写双向链表LinkedList的几个常用功能
查看>>
Spring Boot 单元测试二三事
查看>>
ECS应用管理最佳实践
查看>>
深入理解margin
查看>>
使用clay.js绘制一棵圆形树
查看>>
“被狗啃”的按钮引发的开源社区信任危机
查看>>