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 ...
随机推荐
- YaoLingJump开发者日志(六)
作为一只天才魔法少女狐,不会魔法怎么行?于是我给瑶玲增加了一个技能:魔法弹. 当然,能使用魔法的前提是得有个魔杖,像这样: 魔杖不仅能让瑶玲使用魔法,当瑶玲被攻击时还能提供2s的无敌状态: ...
- QWidget一生,从创建到销毁事件流
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QWidget一生,从创建到销毁事件流 本文地址:http://techieliang ...
- 在linux下如何显示隐藏文件
#显示所有文件(包含隐藏文件)ls -a #只显示隐藏文件l.或者ls -d .* #在XWindow的KDE桌面中在"查看(View)"菜单里选"显示隐藏文件(Show ...
- ManagementClass("Win32_Share")之共享目录
public class ShareFolder { private static readonly Dictionary<uint, string> ReturnDetails = ne ...
- java 基础--switch--003
1,break可以省略吗? default中的可以省略,其他的如果省略会执行下一个case,从下一个case的 break中中断.(case穿透) 2,default一定要在最后吗? 不是,可以在任意 ...
- IOI 98 (POJ 1179)Polygon(区间DP)
很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...
- 【bzoj4229】选择 离线+LCT
题目描述 现在,我想知道自己是否还有选择. 给定n个点m条边的无向图以及顺序发生的q个事件. 每个事件都属于下面两种之一: 1.删除某一条图上仍存在的边 2.询问是否存在两条边不相交的路径可以从点u出 ...
- Linux 文件上传Linux服务器
进入命令行 在图形化桌面出现之前,与Unix系统进行交互的唯一方式就是借助由shell所提供的文本命令行界面(command line interface,CLI).CLI只能接受文本输入,也只能显示 ...
- 深入理解Delete(JavaScript)
深入理解Delete(JavaScript) Delete 众所周知是删除对象中的属性. 但如果不深入了解delete的真正使用在项目中会出现非常严重的问题 (: Following 是翻译 ka ...
- 【题解】HNOI2016序列
也想了有半天,没有做出来……实际上做法确实也是十分精妙的.这里推荐一个blog,个人认为这位博主讲得挺好了:Sengxian's Blog; 感觉启示是:首先要加强对莫队算法 & ST表的熟练 ...