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. HDU 4302 Holedox Eating (线段树模拟)

    题意:一个老鼠在一条长度为L的直线上跑,吃蛋糕,老鼠只能沿直线移动.开始时没有蛋糕,老鼠的初始位置是0. 有两个操作,0 x 代表在位置x添加一个蛋糕: 1 代表老鼠想吃蛋糕.老鼠每次都会选择离自己最 ...

  2. 【转】Backbone.js学习笔记(一)

    文章转自: http://segmentfault.com/a/1190000002386651 基本概念 前言 昨天开始学Backbone.js,写篇笔记记录一下吧,一直对MVC模式挺好奇的,也对j ...

  3. CentOS Openvpn搭建以及 linux&&windows客户端的连接

    本文参考:http://www.centoscn.com/CentosServer/test/2014/1120/4153.html 一. Server安装准备     (CentOS release ...

  4. Mishka and Contest(模拟水题)

    Mishka started participating in a programming contest. There are nn problems in the contest. Mishka' ...

  5. wwnjld团队第二轮迭代成员分数

    2014-01-05 第二轮迭代团队内成员分数如下(依据分数分配规则以及团队会议协商所得结果): 吴渊渊 23 汪仁贵 21.5 高小洲 19.5 聂建 22.5 吕家辉 23.5 程志 10

  6. Java数组课程作业

    设计思路:生成随机数,赋值给数组.再将其求和输出 程序流程图: 源程序代码: import javax.swing.JOptionPane; public class Test { public st ...

  7. Android 平台 HTTP网速测试 案例 API 分析

    作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/25996817 工信部规定的网速测试标准 : 除普通网页测速 ...

  8. java—连连看-实现封装

    1.封装 Chess.java package Linkup; /** * 棋子封装类 * * @author laixl * */ public class Chess { // 图片的 状态 // ...

  9. SQL 跨库查询

    使用SQL查询数据,不仅能查询当前库的数据,还可以跨数据库,甚至跨服务器查询. 下面给大家介绍一下跨服务器查询的步骤(以SQL Server为例): 1,建立数据库链接 EXEC sp_addlink ...

  10. Kafka Streams演示程序

    本文从以下六个方面详细介绍Kafka Streams的演示程序: Step 1: 下载代码 Step 2: 启动kafka服务 Step 3: 准备输入topic并启动Kafka生产者 Step 4: ...