首页 > 数据库技术 > 详细

在centos7上作用mongodb

时间:2015-11-24 23:37:49      阅读:406      评论:0      收藏:0      [点我收藏+]

安装服务端

yum install mongodb-server  

安装客户端

 yum install mongodb

版本
 mongo --version

是否安装了mongodb
which mongod
 history | grep mongo

mkdir -p /data/db/

启动

mongod --dbpath=/data/db --port=27017

关闭,还可用kill

mongod --shutdown

增加日志

mongod --dbpath=/data/db --port=27017 --fork --logpath=/var/log/mongod.log

查看日志

tail -f /var/log/mongod.log

连接客户端

mongo

显示数据库

>show dbs;

创建数据库

>use part9;

插入文档

>db.users.insert({"username": "Sid"});

显示集合

> show collections
system.indexes
users

查询集合中的文档
> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
> db.users.insert({"username": "Zoe", "group": "reporter"});
WriteResult({ "nInserted" : 1 })

> db.users.insert({"username": "Zoe", "group": "reporter"});
WriteResult({ "nInserted" : 1 })
> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "reporter" }

查询文档数量
> db.users.find().count();
2
> db.users.insert({"username": "Zoe", "group": "programmer"});
WriteResult({ "nInserted" : 1 })
> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "reporter" }
{ "_id" : ObjectId("56546d68ac40378400147959"), "username" : "Zoe", "group" : "programmer" }
> db.users.find().count();
3
> db.users.find({"_id" : ObjectId("56546ce3ac40378400147958")});
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "reporter" }

更新文档,但是只修改了一行文档
> db.users.update({"username": "Zoe"}, {$set:{"group": "writer"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "writer" }
{ "_id" : ObjectId("56546d68ac40378400147959"), "username" : "Zoe", "group" : "programmer" }

更新满足条件的所有文档,multi: true
> db.users.update({"username": "Zoe"}, {$set:{"group": "writer"}}, {multi: true});
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })
> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "writer" }
{ "_id" : ObjectId("56546d68ac40378400147959"), "username" : "Zoe", "group" : "writer" }

使用save()进行修改
>db.users.save({"_id" : ObjectId("56546d68ac40378400147959"), "group":"reporter"});

> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "writer" }
{ "_id" : ObjectId("56546d68ac40378400147959"), "group" : "reporter" }

update()有三个参数,save()只有一个参数,两者有区别,注意

 

删除集合中的文档remove()

先构造一个集合如下

> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "writer" }
{ "_id" : ObjectId("56546d68ac40378400147959"), "group" : "reporter" }
{ "_id" : ObjectId("56547d15ac4037840014795a"), "group" : "reporter" }
{ "_id" : ObjectId("56547d18ac4037840014795b"), "group" : "reporter" }
{ "_id" : ObjectId("56547d1bac4037840014795c"), "group" : "reporter" }
{ "_id" : ObjectId("56547d1cac4037840014795d"), "group" : "reporter" }

删除了5条文档

> db.users.remove({"group": "reporter"});
WriteResult({ "nRemoved" : 5 })
> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "writer" }

 

重新构造一个集合

> db.users.find();
{ "_id" : ObjectId("565468b4ac40378400147957"), "username" : "Sid" }
{ "_id" : ObjectId("56546ce3ac40378400147958"), "username" : "Zoe", "group" : "writer" }
{ "_id" : ObjectId("56547e2bac4037840014795e"), "group" : "reporter" }
{ "_id" : ObjectId("56547e2dac4037840014795f"), "group" : "reporter" }
{ "_id" : ObjectId("56547e2fac40378400147960"), "group" : "reporter" }
{ "_id" : ObjectId("56547e30ac40378400147961"), "group" : "reporter" }

增加remove()中的第二个参数,只删除集合中满足条件的第一条文档

> db.users.remove({"group":"reporter"}, true);
WriteResult({ "nRemoved" : 1 })

删除所有的文档

> db.users.remove({});
WriteResult({ "nRemoved" : 5 })
> db.users.find();

删除集合中的所有文档和索引

> db.users.drop();
true

 

在centos7上作用mongodb

原文:http://www.cnblogs.com/herosoft/p/4993284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!