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 前言 ...
随机推荐
- Struts2学习:interceptor(拦截器)的使用
对于需要登陆验证.权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护.struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组 ...
- @Autowired与@Resource 详细诠释和区别(附带例子)
@Autowired 与@Resource:1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配( ...
- Java8之分组
数据库中根据多个条件进行分组 ) from tableA group by a, b 现在不使用sql,而直接使用java编写分组,则通过Java8根据多个条件进行分组代码如下: List<Us ...
- linux文件基本权限-基本权限的修改
基本权限的修改 当我们在linux或unix系统的terminal输入"ls -l"命令时,将输出文件的详细信息.第一列,如“drwxr-xr-x”就是文件的权限信息. yinti ...
- Element-UI安装和项目开发
方法一:引入CDN 使用起来最简单的方法,直接引入CDN就可以工作了 <!-- 引入样式 --> <link rel="stylesheet" hre ...
- WPF DataGrid 数据绑定、样式、分页、增删改查,连接Access数据库
先上效果图: XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...
- Freemarker入门
Freemarker入门 工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId& ...
- ubuntu上virtualbox无法找到usb设备【解决】
How to set up USB for Virtualbox? USB in different versions of Virtual Box For use of USB in Virtual ...
- mysql错误:Column count doesn't match value count at row 1
mysql错误:Column count doesn't match value count at row 1 mysql错误:Column count doesn't match value cou ...
- 解决在V2.0中子组件使用v-model接收来自父组件的值异常
当我们使用父组件向子组件传值,当子组件中是v-model使用该值时会报:[Vue warn]: Avoid mutating a prop directly since the value will ...