创建用户
db.createUser({user: "abc",pwd: "abc123",roles: [ { role: "readWrite", db: "test" } ]});
db.system.users.remove({user:"abc"})
 
MongoDB导出数据
mongoexport -d service -c speech -o file --type csv -f id,time
mongoexport -d service -c speech -o file --type json -f id,time
 
mongoexport -d service -c speech -o file.json --type json -f id,time -q '{"time": {$gt:1483200000000}}' #大于等于2017年
mongoexport -d service -c speech -o file.csv --type csv -f id,time -q '{"time": {$gt:1483200000000}}' #大于等于2017年
 

建立全文索引
db.imooc_collections.ensureIndex({article:"text"})
db.imooc_collections.ensureIndex({"$**":"text"})

db.imooc_collections.find({$text: {$search: "aa"}})
db.imooc_collections.find({$text: {$search: "aa bb cc"}}) 或查询
db.imooc_collections.find({$text: {$search: "aa bb -cc"}}) 排除cc查询
db.imooc_collections.find({$text: {$search: "\"aa\" \"bb\" \"cc\"}}) 且查询

相似度排序
db.imooc_collections.find({$text: {$search: "aa bb"},{score: {$meta: "textScore"}}})
db.imooc_collections.find({$text: {$search: "aa bb"},{score: {$meta: "textScore"}}}).sort({score: {$meta: "textScore"})

MongoDB更新字段及其子字段

collection.update({'account_id': field[0].strip()}, {"$set":{'account_id': field[1].strip(), "waibu":"waibu2", "person_info.account_id": field[1].strip()}},False,True)

数据源为:

{
"_id" : ObjectId("591dc17c10a5664726235e70"),
"account_id" : "5A76F4CAFD5771C98659D6982DA34BF0",
"waibu" : "waibu2",
"person_info" : {
"account_id" : "5A76F4CAFD5771C98659D6982DA34BF0",
"test" : "abc"
}
}

MongoDB实现groupby count操作

db.l2.aggregate([{$group : {_id : "$account_id", num_tutorial : {$sum : }}}])

MongoDB实现groupby avg操作

db.speech.aggregate([{$group:{_id:"$domain",avger:{$avg:"$time"}}}])

MongoDB实现单字段分段查询:

db.client_agent.find({"time":{'$gte':,'$lt':}}).count()

MongoDB实现and操作

db.speech.find({"$and":[{"time":{'$gte':,'$lt':}},{"province":"西藏"}]}).pretty().count()

MongoDB实现distinct操作

db.speech.distinct('city',{"time":{"$gte":}})

MongoDB实现count(distinct city)操作

db.runCommand({"distinct":"speech","key":"city","query":{"time":{"$gte":}}}).values.length

待续。。。

MongoDB随笔的更多相关文章

  1. MongoDB随笔3:使用索引

    创建索引的语句很简单. 1.单键索引的创建:db.test.ensureIndex({name:1},{name:'index_name'}) 2.复合索引的创建:db.test.ensureInde ...

  2. [转]MongoDB随笔2:使用查询

    转自:http://www.cnblogs.com/yangecnu/archive/2011/07/16/2108450.html 一.通过查询获取数据 在深入讨论查询之前,首先来了解一下查询返回的 ...

  3. 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引

    数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA

  4. linux环境下安装mongodb

    最近有用到mongodb,顺便找到了以前的指南,顺便写一篇随笔,以后或许有用到的地方. 第一步:下载mongodb的linux版本,mongodb-linux-x86_64-3.2.4.tgz(去官网 ...

  5. 通过MongoDB的samus驱动实现基本数据操作

    一.MongoDB的驱动 MongoDB支持多种语言的驱动: 在此我们只介绍 C# 的驱动.仅C#驱动都有很多种,每种驱动的形式大致相同,但是细节各有千秋,因此代码不能通用.比较常用的是官方驱动和sa ...

  6. 8天学通MongoDB

    随笔分类 - MongoDB 双十一来了,别让你的mongodb宕机了 摘要: 好久没过来吹牛了,前段时间一直赶项目,没有时间来更新博客,项目也终于赶完了,接下来就要面临双十一这场惊心动魄的处女秀考验 ...

  7. 如何在.Net中使用MongoDB

    最近在研究mongodb,针对.net 中使用mongodb的文章要么是早期的驱动版本,要么资料很少,所以写个随笔记录一下 本文主要记录 1.什么是MongoDB 2.MongoDB windows ...

  8. MongoDB入门系列(一):基础概念和安装

    概述 MongoDB是目前非常流行的一种非关系型数据库,作为入门系列的第一篇本篇文章主要介绍Mongdb的基础概念知识包括命名规则.数据类型.功能以及安装等. 环境: OS:Windows Versi ...

  9. MongoDB入门系列(二):Insert、Update、Delete、Drop

    概述 本章节介绍Insert.Update.Delete.Drop操作基本语法. 环境: Version:3.4 insert insert()基本语法如下: db.collection.insert ...

随机推荐

  1. 一些LR的经验,讲的还不错

    https://blog.csdn.net/Dinosoft/article/details/50492309 记录一下.

  2. 初学数位DP--hdu 2089

    其实是做topcoder的时候碰到不会的题,看人家说要用数位dp,所以拿http://acm.hdu.edu.cn/showproblem.php?pid=2089来学习了一下 数位dp适合在一段数的 ...

  3. 触摸事件【MotionEvent】简介

    MotionEvent简介 当用户触摸屏幕时,将创建一个MontionEvent对象,MotionEvent包含了关于发生触摸的位置.时间信息,以及触摸事件的其他很多细节. Android 将所有的输 ...

  4. FatSecret Platform API

    在现阶段饮食类的APP发展的非常迅猛,尤其在校园中,学生只需要凭借一个手机就能买到自己想要的食物,真正做到了足不出户.可是如果我们想独立完成一个app就需要有相应的数据支持,这里给大家介绍一个国外的开 ...

  5. 实现微信小程序的3rd_session

    function 3rd_session($len) { $fp = @fopen('/dev/urandom','rb'); $result = ''; if ($fp !== FALSE) { $ ...

  6. 使用libsvm对MNIST数据集进行实验---浅显易懂!

    原文:http://blog.csdn.net/arthur503/article/details/19974057 在学SVM中的实验环节,老师介绍了libsvm的使用.当时看完之后感觉简单的说不出 ...

  7. UNIX网络编程读书笔记:基本UDP套接口编程

    概述 使用UDP编写的一些流行的应用程序有:DNS(域名系统).NFS(网络文件系统)和SNMP(简单网络管理协议). 如下图所示,给出了典型的UDP客户/服务器程序的函数调用: 客户不与服务器建立连 ...

  8. Java之进阶(1) -《Effective Java》

    第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇 ...

  9. 升级Eclipse出错的解决办法“Updating Software” has encountered a problem: An error occurred while uninstalling

    运行 eclipse 把 eclipse exe 修改为 eclipse.exe.back 再运行 updates 升级成功

  10. 登陆Oracle EBS的Form遇到问题Internet Explorer has modified this page to help prevent cross-site scripting

    登陆Oracle EBS的Form遇到问题Internet Explorer has modified this page to help prevent cross-site scripting 今 ...