MongoDB入门(4)- MongoDB日常操作
MongoDB客户端
MongoDB有很多客户端
MongoVue
Robomongo
MongoDB命令行
启动mongo shell
在windows下,双击mongo.exe可以启动mongo shell
查询库、表及选择库
查询所有库命令:
show dbs
应用某一个db
use jxs_database
查询此db里面所有collection
show collections
查询数据
去重查询
db.getCollection('MonitorInfo').distinct("Level")
查询所有数据
db.asset_entity.find()
查询一条数据
db.asset_entity.findOne()
查询条数
db.asset_entity.find()
查询某一条符合条件的数据
db.asset_entity.find({"voucher_number":"5555"})
只查询某一列数据
db.asset_entity.find({},{"change_time":true})
db.asset_entity.findOne({"voucher_number":"444345"})
查询符合条件的某N列数据
db.asset_entity.find({"voucher_number":"5555"},{"change_time":true,})
db.asset_entity.find({"voucher_number":"5555"},{"change_time":true,"voucher_number":true})
查询在18~30岁(含)的用户
db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})
要查找在2007年1月1日前注册的人,可以像下面这样:
>start = new Date("01/01/2007")
>db.users.find({"registered" : {"$lt" : start}})
查找排序
db.getCollection('ReportLog').find({}).sort({"createtime":1})
like查找
db.getCollection('ReportClientMongoLog').find({"Msg":/.*工作.*/}).count()
db.getCollection('ReportClientMongoLog').find({"Msg":/.*闲置.*/}).count()
db.getCollection('SalaryEntity').find({"Month" : "201601"})
db.getCollection('SalaryEntity').find({"Month" : "201601", "DepartName" : "运维与管理软件部"})
db.getCollection('ReportClientMongoLog').find({"Ip":"192.168.88.136"},{"CreateTime":true}).sort({"CreateTime":-1})
db.getCollection('ReportClientMongoLog').find().count()
db.getCollection('SalaryEntity').find({"Month" : "201601"},{"DepartName":true})
删除数据
删除符合条件的数据
db.asset_entity.remove({"voucher_number":"5555"})
db.getCollection('MyVersion').remove({})
db.getCollection('ReportHeartBeatKey').remove({"Ip":/.*88.*/})
更新数据
db.asset_check.update({"asset_num":"NUM19"},{"$set":{"model":"x230i"}},false,true)
如果没有后面两个参数,则只更新一行数据。
db.getCollection('PersonBaseInfo').update(
// query
{
"DepartName" : "MIS与互联网部"
},
// update
{
"$set":
{
"DepartName" : "互联网与无线电项目部"
}
},
// options
{
"multi" : true, // update only one document
"upsert" : false // insert a new document, if no existing document match the query
}
);
插入数据
插入一条数据
db.asset_type.insert({"serialId":"161261","name":"mytest","pid":"16126"})
更改表结构
mongo里面没有表结构这个概念,现在采用类似关系型数据库的形式来描述。如果想去掉collection里面的一个key,可以采用以下命令:
db.UserEntity.update({},{$unset:{Mail:1}},false,true);
上面的命令从表UserEntity中删除一个字段Mail。
关于unset的具体说明
$unset
The $unset operator deletes a particular field. Consider the following syntax:
{ $unset: { <field1>: "", ... } }
The specified value in the $unset expression (i.e. "") does not impact the operation.
To specify a <field> in an embedded document or in an array, use dot notation.
Behavior
If the field does not exist, then $unset does nothing (i.e. no operation).
When used with $ to match an array element, $unset replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.
Example
The following update() operation uses the $unset operator to remove the fields quantity and instock from the first document in the products collection where the field sku has a value of unknown.
db.products.update(
{ sku: "unknown" },
{ $unset: { quantity: "", instock: "" } }
)
SEE ALSO
MongoDB入门(4)- MongoDB日常操作的更多相关文章
- Mongodb入门并使用java操作Mongodb
转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...
- MongoDB入门---文档查询操作之条件查询&and查询&or查询
经过前几天的学习之路,今天终于到了重头戏了.那就是文档查询操作.话不多说哈,直接看下语法: db.collection.find(query, projection) query :可选,使用查询操作 ...
- MongoDB入门 和nodejs操作
简介 MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshel ...
- [MongoDB]入门操作
摘要 在工作中也经常使用mongodb,每次遇到新的操作都需要去查,比较麻烦,准备在博客中系统的学习一下mongodb.首先在本地安装mongodb环境,可以下载一个windows的版本. 官网地址 ...
- mongoDB - 日常操作三
MongoDB 进程控制 进程控制 db.currentOp() # 查看活动进程 db.$cmd.sys.inprog.findOne() # 查看活动进程 与上面一样 opid # 操作进程号 o ...
- C#操作MongoDB入门
1.MongoDB安装及配置 (1)下载: mongodb官网 https://www.mongodb.com/download-center 进入官网下载页,你会发现版本都是windo ...
- MongoDB入门---聚合操作&管道操作符&索引的使用
经过前段时间的学习呢,我们对MongoDB有了一个大概的了解,接下来就要开始使用稍稍深入一点的东西了,首先呢,就是MongoDB中的聚合函数,跟mysql中的count等函数差不多.话不多说哈,我们先 ...
- MongoDB入门---文档查询之$type操作符&limit方法&skip方法&简单排序(sort)操作
上一篇文章呢,已经分享过了一部分查询操作了,这篇文章呢?就来继续分享哈.接下来呢我们直接看MongoDB中的$type操作符哈.它呢是基于BSON类型来检索集合中匹配的数据类型,并且返回结果,在Mon ...
- MongoDB入门---文档操作之增删改
之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作.话不多说,大家来看正文. 首先来看一下它的数据结构: ...
- MongoDB - 日常操作二
MongoDB 开启认证与用户管理 ./mongo # 先登录 use admin # 切换到admin库 db.addUser(") # 创建用户 db.addUser('zhansan ...
随机推荐
- Ext JS 6学习文档-第5章-表格组件(grid)
Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...
- Thunder团队第一周 - Scrum会议7
Scrum会议7 小组名称:Thunder 项目名称:爱阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...
- Java常用类之String
String 类: 1. java.lang.String 类代表不可变的字符序列: 2. “XXX” 为该类的一个对象: 3. String 类的常用构造方法: ① String(String o ...
- lintcode-197-排列序号
197-排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列 [1,2,4] 是第 1 个排列. 思路 参考http://www ...
- lintcode-187-加油站
187-加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]. 你有一辆油箱容量无限大的汽车,现在要从 ...
- <Effective C++>读书摘要--Templates and Generic Programming<一>
1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...
- bootstrap列表添加滚动条
有时候列表中数据过多,导致超出页面,影响视觉感受.这时我们需要添加一个滚动条. 初始状态如图: 代码如下: <ul class="list-group"> <li ...
- grid++json页面数据传入
最近遇到一个问题,就是要用Grid++做页面数据报表打印,但是翻了Grid++文档就是没有直接从页面上传数据的,都是要加载txt文档,填写txt文档的url.自己尝试直接页面上传JSON数据到Grid ...
- %pylab ipython 中文
格式:%pylab [--no-import-all] [gui] 该命令会在ipython或notebook环境中自动加载numpy和matplotlib库,跟以下语句功能一致 import num ...
- EJB介绍
EJB定义: 被称为java企业bean,服务器端组件,核心应用是部署分布式应用程序.用它部署的系统不限定平台.实际上EJB是一种产品,描述了应用组件要解决的标准. 标准: 可扩展 (Scalable ...