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 前言 ...
随机推荐
- 《linux性能及调优指南》 3.5 网络瓶颈
3.5 Network bottlenecks A performance problem in the network subsystem can be the cause of many prob ...
- for循环中进行联网请求数据、for循环中进行异步数据操作,数据排序错乱问题解决;
for循环中进行联网请求数据,由于网络请求是异步的,第一个网络请求还没有回调,第二次第三次以及后续的网络请求又已经发出去了,有可能后续的网络请求会先回调:这时我们接收到的数据的排序就会错乱:怎么才能让 ...
- 点不到的NO
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- web前端全栈学习之路
web前端全栈学习之路 --- 陆续更新中 一.HTML相关 1.HTML常用标签:http://www.cnblogs.com/wyb666/p/8733699.html 2.HTML5基础: 3. ...
- Structs 2 session 学习
后台获取 值类型 request.getSession().setAttribute("username", user.getUserName()); 对象类型 reque ...
- JAVA 操作mysql 存储 调用
HashMap map = new HashMap(); map.put("a", 9); map.put("b", ""); List&l ...
- django之ForNode是如何渲染的
django的模板编译后变成一般text.tag.varible,然后根据上下文进行渲染. class ForNode(Node): child_nodelists = ('nodelist_l ...
- 将Oracle中的表结构导出到word
语句如下: SELECT t1.Table_Name AS "表名称",t3.comments AS "表说明", t1.Column_Name AS &quo ...
- 用Redis实现分布式锁 与 实现任务队列
这一次总结和分享用Redis实现分布式锁 与 实现任务队列 这两大强大的功能.先扯点个人观点,之前我看了一篇博文说博客园的文章大部分都是分享代码,博文里强调说分享思路比分享代码更重要(貌似大概是这个意 ...
- 使用STM32CubeMX生成USB_HOST_HID工程[添加对CAPS_LOCK指示灯的控制][SetReport]
在之前(使用STM32CubeMX生成USB_HOST_HID工程)的基础上进行修改 在结合之前在pc上的测试 USB之HID类Set_Report Request[调试手记1] 测试代码如下: /* ...