MongoDB入门(4)- MongoDB日常操作

摘要:
MongoDB客户端MongoDB有很多客户端MongoVueRobomongoMongoDB命令行在Windows下启动mongoshell,双击mongo.Exe即可启动mongoShell查询库、表和选择库。查询所有库。命令:showdbs应用dbusejxs_数据库查询此数据库中的所有集合showcollections,查询数据,然后重新查询db.getCollection

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日常操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇软件系统设计思路我的学习笔记,侧信道攻击,从喊666到入门之——波形采集下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MongoDB 高级查询_aggregate聚合管道

MongoDB 聚合管道(AggregationPipeline) 使用聚合管道可以对集合中的文档进行变换和组合。实际项目应用主要是表关联查询、数据的统计。 MongoDB 中使用 db.COLLECTION_NAME.aggregate([{<stage>},...]) 方法 来构建和使用聚合管道。下面是官网给的实例,感受一下聚合管道的用法。...

[转]mongodb与mysql相比的优缺点

原文地址:http://blog.sina.com.cn/s/blog_966e430001019s8v.html 与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问速度:举例来说,在传统的关系型数据库中,一个COUNT类型的操作会锁定数据集,这样可以保证得到“当前”情况下的精确值。这在某些情况下,例 如通过ATM查看账户...

mongodb创建用户密码

自从接手这个项目以来,真的被坑惨了。测试服的mongodb经常被删,原因是没有设置账户密码,相应的端口也没有限。只要知道连接地址谁都可以删库。这要归功于mongodb的默认机制。就这样竟然度过了两年多,我一来就特么删库。 以mongo3.4.24为例,首先在mongo.conf中开启认证。还没创建用户密码的可以创建用户了再开启。 security:...

MongoDB 的索引到底是使用 B+ 树还是 B 树

先上结论,根据官网的说法是 B 树 然而笔者看到一篇,云栖社区-MongoDB 为什么使用B-树而不是B+树?,里面有人如下回答 实际是B+树,这个在2018年元旦北京的MongoDB专场,我问了WiredTiger引擎的作者,他也确认了是B plus Tree。虽然官方文档写了B树。 现在有些觉得迷惑了,要是有人知道,请留言告诉我好么。 由于第二个观点...

mongodb的增、删、改、插的一个实例

创建一个学生信息表(至少包含:姓名,性别,成绩,年龄) 1、写入十五条不同的数据 db.students.insertMany([{ name: "bob", age: 16, sex: "male", grade: 95},{ name: "ahn", age: 18, sex: "female", grade: 45},{ name: "xi", ag...

Atitit mysql redis mongodb 性能指标 目录 1.1. 二、 性能比较 1 1.2. 性能极高- Redis能读的速度是110000次/s,写的速度是81000次/s 2 1

Atitit mysql redis mongodb 性能指标   目录 1.1. 二、 性能比较 1 1.2. 性能极高- Redis能读的速度是110000次/s,写的速度是81000次/s 2 1.3. Mysql单表数据量达到5-10G时会出现明细的性能降级 2 1.4. Mongodb 最起码是插入速度是mysql俩倍数 2 1.5. 查询速度就...