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. Python3 Tkinter-Button

    1.绑定事件处理函数 from tkinter import * def hello(): print('Hello!') root=Tk() button=Button(root,text='cli ...

  2. 2.安装hdfs yarn

    下载hadoop压缩包设置hadoop环境变量设置hdfs环境变量设置yarn环境变量设置mapreduce环境变量修改hadoop配置设置core-site.xml设置hdfs-site.xml设置 ...

  3. Linux中常用的关机和重新启动命令

    hutdown.halt.reboot以及init,它们都可以达到关机和重新启动的目的,但是每个命令的内部工作过程是不同的,下面将逐一进行介绍. 一.shutdown shutdown命令用于安全关闭 ...

  4. 上层应用与wpa_supplicant,wpa_supplicant与kernel 相关socket创建交互分析

    单独拿出来,分析以下上层应用与wpa_supplicant   wpa_supplicant与kernel 的socket交互. 关联上层应用与wpa_supplicant的socket的创建.连接流 ...

  5. Python中的eval

    Python中的eval方法接受一个字符串参数,并且把字符串里面的内容当成Python代码来执行: eval的缺点是执行速度慢,并且会有安全风险

  6. Thunder团队第一周 - Scrum会议6

    Scrum会议6 小组名称:Thunder 项目名称:爱阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  7. Internet History,Tecchnology and Security

    Internet History Internet Technologe Internet Secure

  8. win8安装Ubuntu14

    概述: 1.复制安装镜像和启动文件到FAT32分区 2.查找出FAT32分区的分区号,修改启动配置文件 3.启动FAT32分区的安装镜像,开始安装 UEFI Win7/8/Ubuntu 硬盘安装Ubu ...

  9. Web界面和Winform界面生成,代码生成工具

    在上面一篇随笔<代码生成工具之界面快速生成>介绍了代码生成工具Database2Sharp的界面生成操作,其中介绍了Web界面(包括列表界面.内容显示.内容编辑界面的生成,另外还介绍了Wi ...

  10. ASP.Net MVC+Ibaties架构

    1.配置Ibaties首先在DLL引用中添加Ibaties相关引用:IBatisNet.Common.dll;IBatisNet.Common.Logging.Log4Net.dll;IBatisNe ...