在centos7上作用mongodb
安装服务端
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的更多相关文章
- 在 CentOS7 上安装 MongoDB
在 CentOS7 上安装 MongoDB 1 通过 SecureCRT 连接至 CentOS7 服务器: 2 进入到 /usr/local/ 目录: cd /usr/local 3 在当前目录下创建 ...
- (转)在 CentOS7 上安装 MongoDB
在 CentOS7 上安装 MongoDB 1 通过 SecureCRT 连接至 CentOS7 服务器: 2 进入到 /usr/local/ 目录: cd /usr/local 3 在当前目录下创建 ...
- 在centos7上搭建mongodb副本集
1.安装副本集介绍 副本集(Replica Set)是一组MongoDB实例组成的集群,由一个主(Primary)服务器和多个备份(Secondary)服务器构成.通过Replication,将数据的 ...
- 在阿里云ECS CentOS7上部署基于MongoDB+Node.js的博客
前言:这是一篇教你如何在阿里云的ECS CentOS 7服务器上搭建一个个人博客的教程,教程比较基础,笔者尽可能比较详细的把每一步都罗列下来,包括所需软件的下载安装和域名的绑定,笔者在此之前对Linu ...
- 在CentOS7上安装JDK1.8
在CentOS7上安装JDK1.8 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中: cd /usr/local/ 3 创建目录 to ...
- centos7下搭建 MongoDB -01
距离上次写的一篇mongoDB搭建已经有一年多的时间了,刚好这次在公司搭建好在centos7下的mongodb搭建,简单的做一个记录吧 mongo 是一个基于分布式文件存储的数据库,数据主要存储在磁盘 ...
- (转)Centos7上部署openstack ocata配置详解
原文:http://www.cnblogs.com/yaohong/p/7601470.html 随笔-124 文章-2 评论-82 Centos7上部署openstack ocata配置详解 ...
- window系统上实现mongodb副本集的搭建
一.问题引出 假设我们生产上的mongodb是单实例在跑,如果此时发生网络发生问题或服务器上的硬盘发生了损坏,那么这个时候我们的mongodb就使用不了.此时我们就需要我们的mongodb实现高可用, ...
- 在centos7上安装Jenkins
在centos7上安装Jenkins 安装 添加yum repos,然后安装 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins ...
随机推荐
- RDIFramework.NET ━ .NET快速信息化系统开发框架钜献 V2.9 版本震撼发布
RDIFramework.NET ━ .NET快速信息化系统开发框架钜献 V2.9 版本震撼发布 全新体验.全新感觉.2015钜献! 继上个版本“RDIFramework.NET V2.8版本发布”5 ...
- [转] PHP计算两个坐标之间的距离, Calculate the Distance Between Two Points in PHP
Calculate the Distance Between Two Points in PHP There are a lot of applications where it is useful ...
- Centos 安装 MySql
下载mysql下载链接:http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-i686.tar.gz 下载后 ...
- windows系统调用 遍历进程的虚拟地址
#include "iostream" #include "windows.h" #include "shlwapi.h" #include ...
- 关于ebox
看了介绍,觉得挺不错的东西,希望能够一路走下去 老话题STM32编程,新思路,一样是编程,味道却大有不同.这就是STM32之eBox编程.让你提议不一样的编程,让开发快到你意想不 ...
- MongoDB数据库基本用法
show dbs:显示数据库列表 show collections:显示当前数据库中的集合(类似关系数据库中的表) show users:显示用户 use <db name>:切换当前 ...
- 面试题-链表反转c实现
// ListReverse.cpp : Defines the entry point for the console application.// #include "stdafx.h& ...
- mongodb 安装、开启服务 和 php添加mongodb扩展
1.下载mongodb:https://www.mongodb.org/downloads#production (https://www.mongodb.org/dl/win32) 2.安装.配置 ...
- Calendar日历小程序
//有待完善,有点bugpackage com.sunshine.framework.calendar.model;import java.util.Calendar;/** * * <p> ...
- [转](二)unity4.6Ugui中文教程文档-------概要-UGUI Canvas
大家好,我是孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityma ...