技术:mongo3.2.0
运行环境:centos7 + mongo 3.2.0
demo功能:提供基于mongo命令的集合操作
#添加记录
> db.hellokitty.insertOne({"_class" : "com.demoworld.entity.HelloKitty", "name" : "francis lee", "age" : 45})
{
"acknowledged" : true,
"insertedId" : ObjectId("5963fa28f810296c59e0bc72")
}
#查看添加结果
> db.hellokitty.find({name:'francis lee'})
{ "_id" : ObjectId("5963fa28f810296c59e0bc72"), "_class" : "com.demoworld.entity.HelloKitty", "name" : "francis lee", "age" : 45 }
>
#查询全部数据, 可以分页查看全部数据
db.hellokitty.find()
#限制查询前几条
> db.hellokitty.find().limit(2)
{ "_id" : "3962de4b-f095-4008-a320-fad781a17ca6", "_class" : "com.demoworld.entity.HelloKitty", "name" : "francis", "age" : 16 }
{ "_id" : "a5690a44-3248-46a6-b2fc-f1ae30c11846", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Arthur Allen", "age" : 3 }
#按照某个字段匹配查询
> db.hellokitty.find({name:'francis', age:16})
{ "_id" : "3962de4b-f095-4008-a320-fad781a17ca6", "_class" : "com.demoworld.entity.HelloKitty", "name" : "francis", "age" : 16 }
#查询年龄大于10岁的信息
db.hellokitty.find({age: {$gt : 10}}).sort({age:1})
#其他操作符类比查询
$gt -------- greater than
$gte --------- gt equal
$lt -------- less than
$lte --------- lt equal
$ne ----------- not equal
#按照age字段升序输出
db.hellokitty.find().sort({age:1})
#按照age字段升序, 再按照name降序输出
db.hellokitty.find().sort({age:1, name:1})
#输出第1页数据, 每页2条
db.hellokitty.find().skip(0).limit(2)
{ "_id" : "3962de4b-f095-4008-a320-fad781a17ca6", "_class" : "com.demoworld.entity.HelloKitty", "name" : "francis", "age" : 16 }
{ "_id" : "a5690a44-3248-46a6-b2fc-f1ae30c11846", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Arthur Allen", "age" : 3 }
#输出第2页数据, 每页2条
> db.hellokitty.find().skip(2).limit(2)
{ "_id" : "22f5b57b-aa3d-40e6-9ed2-6e41dd90781a", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Antonio Snyder", "age" : 62 }
{ "_id" : "c326fe5a-5607-4849-822d-f0fbca4daa4c", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Francis Holmes", "age" : 6 }
#输出第3页数据, 每页2条
> db.hellokitty.find().skip(4).limit(2)
{ "_id" : "ad7e0df5-2689-4366-b0e3-ab829762c3b1", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Walter Jackson", "age" : 0 }
{ "_id" : "c136b9a8-c0e3-439b-8c18-d62a8b5719b9", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Martin Jordan", "age" : 6 }
#输出第4页数据, 每页2条
> db.hellokitty.find().skip(6).limit(2)
{ "_id" : "161c1d3c-c8d8-41eb-96cb-0de5150fd646", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Carl Hicks", "age" : 91 }
{ "_id" : "dc768514-83d5-4cb5-8941-8dcfff940484", "_class" : "com.demoworld.entity.HelloKitty", "name" : "Eugene Howard", "age" : 98 }
#全部删除,太危险,这里就不讲了
#按照age字段为0的数据
db.hellokitty.find({age:0})
db.hellokitty.remove({age:0},0)
删除说明:remove(类似where的删除条件, 0=全部数据1=第一条数据)
#查询age=1数据
> db.hellokitty.find({age:1})
#更新查询的结果数据age=111
> db.hellokitty.update({"age":1},{$set:{age:111}},false,true)
WriteResult({ "nMatched" : 7, "nUpserted" : 0, "nModified" : 7 })
参数1:筛选条件
参数2:更新哪些字段
参数3:如果没有筛选到符合条件的记录,是否需要将参数2插入到集合中,默认false,不插入
参数4:默认false,一次更新一条;true一次更新多条,此时参数2需要使用$set操作
#查询是否还有age=1的数据
> db.hellokitty.find({age:1})
#插叙是否有age=111的数据
> db.hellokitty.find({age:111})
欢迎添加微信,互相学习↑↑↑ -_-
白老虎
programming is not only to solve problems, ways to think
grafana 级连 菜单 templating (variables) 配置
rocketmq 集群搭建 (2master + 2slave + 2namesrv)
AI 机器人 抓取 微信 聊天中的 百度网盘 分享地址和密码