MongoDB常用查询,排序,group,SpringDataMongoDB update group
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的更多相关文章
- mongodb常用查询语法
一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...
- MongoDB 常用查询语法
一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...
- 记录MongoDB常用查询
{$and:[{"}}]} // flag不等于1 也不等于0 {$or:[{"flag" :{ $ne:"1"}},{"flag" ...
- mongodb常用查询语句
1.查询所有记录db.userInfo.find();相当于:select* from userInfo; 2.查询去掉后的当前聚集集合中的某列的重复数据db.userInfo.distinct(&q ...
- mongodb常用查询语句(转)
1.查询所有记录 db.userInfo.find();相当于:select* from userInfo; 2.查询去掉后的当前聚集集合中的某列的重复数据db.userInfo.distinct(& ...
- mongodb常用查询
mongo sql 说明 db.users.find() select * from users 从user表中查询所有数据 db.users.find({“username” : “joe”, “a ...
- MySQL之单表查询 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 使用正则表达式查询
MySQL之单表查询 阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER B ...
- Mysql 单表查询-排序-分页-group by初识
Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...
- ysql常用sql语句(12)- group by 分组查询
测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...
随机推荐
- idea一个窗口开启多个项目
在将旧项目重构为微服务架构时,建立了十几个新的项目,对于相互之间存在调用的微服务,开发时都要同时启动,一开始我开了好多个idea窗口,结果电脑卡的不行,后来发现原来可以将项目都放到同一个idea窗口进 ...
- 在Linux上git pull线上仓库代码时,出现error: Your local changes to the following files would be overwritten by merge
在Windows上工作时未出现过该问题,于是通过命令: git diff 查看差异,得到结果: diff --git a/start_crons.sh b/start_crons.sh old mod ...
- 编码,基本数据类型,str索引和切片,for循环
1. 编码 1. 最早的计算机编码是ASCII. 美国人创建的. 包含了英文字母(大写字母, 小写字母). 数字, 标点等特殊字符!@#$% 128个码位 2**7 在此基础上加了一位 2**8 8位 ...
- 占cpu 100%的脚本
#! /bin/sh # filename killcpu.sh if [ $# -ne 1 ] ; then echo "USAGE: $0 <CPUs>|stop" ...
- Strandbeest mechanism and Leg mechanism
I have to say besides computer science study, I'm also interested in Leg mechanism. Share two keywor ...
- Oracle生成关闭外键的SQL语句
select 'alter table ' || t.table_name || ' disable constraint ' || t.constraint_name || ';' from DBA ...
- linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)
代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...
- 试用bus hound来分析STM32CubeMX来生成USB_HID_Mouse工程
Bus Hound (x64). Complements of www.perisoft.net STM32_HID_mouse Device - Device ID (followed by the ...
- Rabbitmq(3) work queues
轮询分发:每个消费者处理的消息是一样的 公平分发:能者多劳 *****公平分发 生产者 package com.aynu.bootamqp.service; import com.aynu.boota ...
- jquery 全选操作
$(function(){ $("#checkedAll").change(function(){ if(this.checked){ $(".checkSingle&q ...