MongoDB查询

指定查询并排序

db.getCollection('location').find({"site.id":"川A12345","site.ts":{$gte : ISODate("2018-11-30T16:00:00.000Z"), $lt :ISODate("2018-12-30T16:00:00.000Z")}}).sort({"site.ts":-1})

  

返回指定字段

db.getCollection('location').find({},{ "site.id" : 1}).pretty()

 

db.getCollection('vehicle.abilities').find({},{"_id":0, "vid":1, "data_range":1})

0表示的是不返回_id字段,1表示返回对应的字段vid,data_range。

 

或运算($or)

db.getCollection('people').find({$or : [{_id:ObjectId("5bae04f2ea403100054bfea4")},{_id:ObjectId("5bae028fea403100054bfea2")}]})

查询id在指定数组结果($in)

db.getCollection('people').find({"_id" :{$in :[ObjectId("5c346a16adbe1d0005b1a290"),ObjectId("5c346a16adbe1d0005b1a291"),ObjectId("5c35a0c024aa9a00065425d6")]}})

  

返回指定字段去重后记录数

db.getCollection('user').distinct("name").length

group查询语句:

db.getCollection('collectionName').aggregate(
[
{ $match : { "labels.site_name" : "川A12345","start":{$gte : ISODate("2018-01-30T16:00:00.000Z"), $lte : ISODate("2018-12-31T16:00:00.000Z")}}},
{ $group : { _id : "$start", number :{ $sum : 1 }}},
{ $sort : { _id : -1 }} ]
)

使用aggregate方法;

$match是查询的方法;

$group 分组字段相关;

$sort 排序。

匹配一个Document中的字段是数组属性其中的值:$elemMatch

比如有如下记录:

{_id: ObjectId("53d760721423030c7e14267d"),
name: 'Tony',
categories: [
{
name: 'coder', }
{
name: 'dotaer', }
{
name: 'cook', }
]
}

现在我想要匹配:name:tony和categories数组中name:dotaer的记录

语句如下:

find({"name":"Tony", "categories":{$elemMatch:{"name":"dotaer"})

匹配记录中有个属性是字符串数组中其中一个字符串,有个属性如下

"tags" : [
"state",
"stat"
]

查询语句如下:

db.getCollection('va').find({tags:"stat"})

强烈建议不要定义一个非对象的属性,如字符串数组,可以定义一个对象数组,对象中只有一个属性,字符串

SpringDataMongoDB

简单查询

 MongoTemplate template = factory.getMongoTemplate(DATABASE_NAME);
Query query = query(where("name").is(name));
People people = template.findOne(query, People.class,"people");

  

update更新

Update update = Update.update("更新字段名", 内容);

template.updateFirst(query, update, entityClass, collectionName);

  

group分组

     Criteria criteria = Criteria.where("start").gte(begin).ite(end).and("name").is(name);
        MatchOperation match = Aggregation.match(criteria);

        GroupOperation group  = Aggregation.group("start").count().as("count");

        // 注group key start会映射成_id,所以要利用project阶段映射回start
ProjectionOperation project = Aggregation.project("count").and("_id").as("start"); Aggregation aggregation = Aggregation.newAggregation(match,group,project); AggregationResults<Map> aggregate = template.aggregate(aggregation, "表名", Map.class);
List<Map> mappedResults = aggregate.getMappedResults();
Criteria 是查询条件,构建对象MatchOperation ,ProjectionOperation ,Aggregation;template是查询的数据库。 
SpringDataMongoDB grouop更加详细的内容:https://blog.csdn.net/fs1360472174/article/details/74081487

MongoDB常用查询,排序,group,SpringDataMongoDB update group的更多相关文章

  1. mongodb常用查询语法

    一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...

  2. MongoDB 常用查询语法

    一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...

  3. 记录MongoDB常用查询

    {$and:[{"}}]} // flag不等于1 也不等于0 {$or:[{"flag" :{ $ne:"1"}},{"flag" ...

  4. mongodb常用查询语句

    1.查询所有记录db.userInfo.find();相当于:select* from userInfo; 2.查询去掉后的当前聚集集合中的某列的重复数据db.userInfo.distinct(&q ...

  5. mongodb常用查询语句(转)

    1.查询所有记录 db.userInfo.find();相当于:select* from userInfo; 2.查询去掉后的当前聚集集合中的某列的重复数据db.userInfo.distinct(& ...

  6. mongodb常用查询

    mongo sql 说明 db.users.find() select * from users 从user表中查询所有数据 db.users.find({“username” : “joe”, “a ...

  7. MySQL之单表查询 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 使用正则表达式查询

    MySQL之单表查询 阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER B ...

  8. Mysql 单表查询-排序-分页-group by初识

    Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...

  9. ysql常用sql语句(12)- group by 分组查询

    测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...

随机推荐

  1. 查看linux的IO占用

    iotop -oPiostat -xm 1 iostat -d -x -kiostat -c top

  2. js控制高度自适应,做到响应式

    //9宫格布局 var prints=window.innerHeight-($('.header').height()+40);//屏幕去掉(头部高度+头部padding) $('.weui-gri ...

  3. 查看Selinux和关闭Selinux

    查看Selinux和关闭Selinux   注:本文非原创文章,转自以下: 链接:https://www.jianshu.com/p/a7900dbf893c 查看SELinux状态: /usr/sb ...

  4. Java file方法的路径特性

    1.在flle方法里,直接写空白的路径,是会默认获取当前Java编译工作空间的路径. 例子如下: package example_1; import java.io.File; import java ...

  5. layer.js关闭子窗口及刷新父窗口

    在需要layer.js弹窗口时,当编辑完窗口内容,需要关闭及刷新父窗口时: $("#senddata").click(function(){var id = $('input[na ...

  6. jl1.如何设置元素的宽高包含元素的边框和内边距

    方法一: 文档地址:http://www.w3school.com.cn/cssref/pr_box-sizing.asp CSS3 box-sizing属性:    box-sizing: bord ...

  7. 4.HTML+CSS制作个月亮

    效果地址:https://codepen.io/flyingliao/pen/LaRmJr?editors=1100 感想:还有缺陷,需后期补充.完善. HTML code: <div clas ...

  8. leetcode990

    class Finder: def __init__(self): self.Parent = [i for i in range(26)] def union(self, p, q): self.P ...

  9. PHP 时间相关操作

    使用函式 date() 实现 <?php echo $showtime=date("Y-m-d H:i:s");?> 显示的格式: 年-月-日 小时:分钟:秒 获得当天 ...

  10. Nginx代理webSocket经常中断的解决方案, 如何保持长连接

    背景 这天气够热的,要处理的事情也够多的.... 1 2 想看解决的,直接 ctrl+f搜索关键字‘配置点’ 开始前交代(想看原因的看这个,个人观点,不代表正确) 解说:今天用nginx反代通讯项目, ...