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日常操作的更多相关文章

  1. Mongodb入门并使用java操作Mongodb

    转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...

  2. MongoDB入门---文档查询操作之条件查询&and查询&or查询

    经过前几天的学习之路,今天终于到了重头戏了.那就是文档查询操作.话不多说哈,直接看下语法: db.collection.find(query, projection) query :可选,使用查询操作 ...

  3. MongoDB入门 和nodejs操作

    简介 MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshel ...

  4. [MongoDB]入门操作

    摘要 在工作中也经常使用mongodb,每次遇到新的操作都需要去查,比较麻烦,准备在博客中系统的学习一下mongodb.首先在本地安装mongodb环境,可以下载一个windows的版本. 官网地址 ...

  5. mongoDB - 日常操作三

    MongoDB 进程控制 进程控制 db.currentOp() # 查看活动进程 db.$cmd.sys.inprog.findOne() # 查看活动进程 与上面一样 opid # 操作进程号 o ...

  6. C#操作MongoDB入门

    1.MongoDB安装及配置 (1)下载:   mongodb官网 https://www.mongodb.com/download-center      进入官网下载页,你会发现版本都是windo ...

  7. MongoDB入门---聚合操作&管道操作符&索引的使用

    经过前段时间的学习呢,我们对MongoDB有了一个大概的了解,接下来就要开始使用稍稍深入一点的东西了,首先呢,就是MongoDB中的聚合函数,跟mysql中的count等函数差不多.话不多说哈,我们先 ...

  8. MongoDB入门---文档查询之$type操作符&limit方法&skip方法&简单排序(sort)操作

    上一篇文章呢,已经分享过了一部分查询操作了,这篇文章呢?就来继续分享哈.接下来呢我们直接看MongoDB中的$type操作符哈.它呢是基于BSON类型来检索集合中匹配的数据类型,并且返回结果,在Mon ...

  9. MongoDB入门---文档操作之增删改

    之前的两篇文章,已经分享过关于MongoDB的集合还有数据库的各种操作,接下来就涉及到最主要的喽,那就是数据方面的操作,在这里叫做文档操作.话不多说,大家来看正文.     首先来看一下它的数据结构: ...

  10. MongoDB - 日常操作二

    MongoDB 开启认证与用户管理  ./mongo # 先登录 use admin # 切换到admin库 db.addUser(") # 创建用户 db.addUser('zhansan ...

随机推荐

  1. 《机器学习实战》笔记——决策树(ID3)

    现在要介绍的是ID3决策树算法,只适用于标称型数据,不适用于数值型数据. 决策树学习算法最大的优点是,他可以自学习,在学习过程中,不需要使用者了解过多的背景知识.领域知识,只需要对训练实例进行较好的标 ...

  2. Bower 显示‘bower ESUDO Cannot be run with sudo’的错误解决方法

    使用 sudo 命令后或者当前用户为 root,执行 bower 相关命令会出现错误: 解决办法: 在命令后面加 --allow-root 例: bower init  --allow-root bo ...

  3. Thunder团队第三周 - Scrum会议2

    Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 胡佑蓉在拍照,所以不在照片中. 参会成员: 王航:http://www.cnblogs. ...

  4. LintCode-105.复制带随机指针的链表

    复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...

  5. 异常--try..catch

    class Program { static void Main(string[] args) { try { object obj = null; int N = (int)obj; } catch ...

  6. 【Python】python 调用c语言函数

    虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了具体流程:c编写相关函数 ,编译成库然后在python中加载这些库,指定调用函数.这些函数 ...

  7. java 中 Stringbuff append源代码浅析

    public synchronized StringBuffer append(String str) {        super.append(str);        return this;  ...

  8. BZOJ 1797 最小割(最小割割边唯一性判定)

    问题一:是否存在一个最小代价路径切断方案,其中该道路被切断? 问题二:是否对任何一个最小代价路径切断方案,都有该道路被切断? 现在请你回答这两个问题. 最小割唯一性判定 jcvb: 在残余网络上跑ta ...

  9. 【bzoj1038】瞭望塔 半平面交

    题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, y1), ( ...

  10. 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp

    题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:对于u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. ...