mongoBD学习(三)插入与查询数据

发布时间:2020-06-17 12:39:37 作者:adelazhu
来源:网络 阅读:389

root@adela-vm:/opt/mongodb_simple# numactl --interleave=all bin/mongod -f conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4904
child process started successfully, parent exiting

root@adela-vm:/opt/mongodb_simple# ./bin/mongo 127.0.0.1:12345
MongoDB shell version: 2.6.9
connecting to: 127.0.0.1:12345/test

> show dbs    //显示有几个数据库
admin  (empty)
local  0.078GB

> use adela   //切换到adela数据库,不需要事先定义
switched to db adela

> show dbs      //adela数据库没有增加内容,所以不现实
admin  (empty)
local  0.078GB
>

RDBMS中的表 对应 mongoDB中的集合collections ,插入内容为json格式
db.集合名(表).insert(json格式)
db.adela_collection.insert({"x":1})

显示所有的集合
> show collections   
adela_collection
system.indexes

插入数据
> db.adela_collection.insert({y:2})
WriteResult({ "nInserted" : 1 })
> db.adela_collection.find()
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f12b41daf13c28f7bdb8"), "y" : 2 }
 
_id是系统自动分配的,也可以自定义,但是 _id 不能重复
> db.adela_collection.insert({a:1,_id:1})
WriteResult({ "nInserted" : 1 })
> db.adela_collection.insert({a:2,_id:1})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: adela.adela_collection.$_id_  dup key: { : 1.0 }"
        }
})

查询adela_collection全表
> db.adela_collection.find()  //不加参数

查询x为1的数据
> db.adela_collection.find({x:1})
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f1d241daf13c28f7bdb9"), "x" : 1 }

循环插入一批数据
> for(i=1;i<11;i++)db.adela_collection.insert({z:i})
WriteResult({ "nInserted" : 1 })

> db.adela_collection.find()
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f12b41daf13c28f7bdb8"), "y" : 2 }
{ "_id" : ObjectId("5514f1d241daf13c28f7bdb9"), "x" : 1 }
{ "_id" : 1, "a" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe59"), "z" : 6 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5a"), "z" : 7 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5b"), "z" : 8 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5c"), "z" : 9 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5d"), "z" : 10 }

计数count
> db.adela_collection.find().count()
14

skip跳过多少条数据
limit限制返回多少条数据
sort使用排序

> db.adela_collection.find()
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f12b41daf13c28f7bdb8"), "y" : 2 }
{ "_id" : ObjectId("5514f1d241daf13c28f7bdb9"), "x" : 1 }
{ "_id" : 1, "a" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe59"), "z" : 6 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5a"), "z" : 7 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5b"), "z" : 8 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5c"), "z" : 9 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5d"), "z" : 10 }

> db.adela_collection.find().skip(4)
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe59"), "z" : 6 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5a"), "z" : 7 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5b"), "z" : 8 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5c"), "z" : 9 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5d"), "z" : 10 }

> db.adela_collection.find().skip(4).limit(5)
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }

> db.adela_collection.find().skip(4).limit(5).sort({z:1})
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }

推荐阅读:
  1. 按条件查询与删除数据-----HBase(伪分布)学习笔记
  2. mongoBD学习(二)客户端的连接

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

插入 mongodb 查询数据

上一篇:Python爬虫一般会用什么框架?常见的五种框架介绍!

下一篇:bootstrap-进度条

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》