摘要:一按照等條件組合查詢,同時(shí)添加和原生二執(zhí)行操作,更新單個(gè)文檔原生三通過(guò)命令更新文檔并且返回更新之后的文檔只能作用于單個(gè)文檔原生四聚合操作根據(jù)某一字段,并且將文檔中的某一字段合并到數(shù)組中,最后取數(shù)組中的第一個(gè)元素原生五數(shù)組查詢,在某個(gè)中包
一、按照in、eq、lte等條件組合查詢,同時(shí)添加sort和limit
1、原生
db.message.find( { receiverRoleId: {$in: [1381073, 1381073]}, resourceType:3, sendTime: {$lte: 1523355918300} }) .sort({sendTime: -1 }) .limit(10);
2、spring data mongoDB
Criteria criteria = Criteria.where("receiverRoleId").in(receiverRoleIds) .and("readState").is(readState.getIndex()) .and("sendTime").lte(sendTime); Query query = Query.query(criteria); query.with(new Sort(Sort.Direction.DESC, "sendTime")); query.limit(count); mongoTemplate.find(query, Notification.class);
二、執(zhí)行update操作,更新單個(gè)文檔
1、原生
db.message.update( {_id: 586537, readState: 2}, {$set: {readState: 1}}, {multi: false} );
2、spring data mongoDB
Criteria criteria = Criteria.where("_id").is(id).and("readState").is(ReadState.UNREAD.getIndex()); Query query = Query.query(criteria); Update update = Update.update("readState", ReadState.READ.getIndex()); mongoTemplate.updateFirst(query, update, Notification.class);
三、通過(guò)findAndModify命令更新文檔并且返回更新之后的文檔(只能作用于單個(gè)文檔)
1、原生
db.message.findAndModify({ query: {_id: 586537, readState: 2}, update: {$set: {publishType: 1}}, new: true });
2、spring data mongoDB
Query query = Query.query(Criteria.where("_id").is(2).and("readState").is(2)); Update update = Update.update("publishType", 1); Notice updateResult = mongoTemplate.findAndModify( query, update, FindAndModifyOptions.options().returnNew(true), Notice.class );
四、聚合操作(根據(jù)某一字段group,并且將文檔中的某一字段合并到數(shù)組中,最后取數(shù)組中的第一個(gè)元素)
1、原生
db.message.aggregate([ { $match: {toAffairId : {$in: [590934, 591016]}} }, { $sort: {sendTime: -1} }, { $group: {_id: "$toAffairId", contents: {$push: "$content"}} }, { $project: {_id: 0, "affaiId": "$_id", contents: {$slice: ["$contents", 1]} } } ]);
2、spring data mongoDB
Criteria criteria = Criteria.where("toAffairId").in(affairIds); Aggregation aggregation = Aggregation.newAggregation( match(criteria), sort(Sort.Direction.DESC, "sendTime"), group("toAffairId").push("content").as("contents"), AggregationResultsresults = mongoTemplate.aggregate( aggregation, collectionName, MobileDynamicMessageDataModel.class );
五、數(shù)組查詢,在某個(gè)document中包含數(shù)組,對(duì)數(shù)組進(jìn)行過(guò)濾,并返回?cái)?shù)組中第一個(gè)符合條件的元素
1、原生
db.audit_record.find( { criteriaAuditRecords: {$elemMatch: {personalAuditIds: {$in: [12180209]}}} } ).project({"_id":1, "criteriaAuditRecords.$":1});
2、spring data mongoDB
Criteria criteria = Criteria.where("criteriaAuditRecords").elemMatch(Criteria.where("personalAuditIds").in(personalAuditId)); Query query = Query.query(criteria); query.fields().include("_id").include("criteriaAuditRecords.$"); return mongoTemplate.findOne(query, AuditRecord.class);
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/19480.html
摘要:介紹非關(guān)系型數(shù)據(jù)是文檔型數(shù)據(jù),文檔是獨(dú)立的實(shí)體,文檔數(shù)據(jù)庫(kù)不適用于關(guān)聯(lián)關(guān)系明顯的數(shù)據(jù)提供了三種方式在應(yīng)用中使用通過(guò)注解實(shí)現(xiàn)對(duì)象文檔映射使用實(shí)現(xiàn)基于模板的數(shù)據(jù)庫(kù)訪問(wèn)自動(dòng)化的運(yùn)行時(shí)生成功能注解將類型映射為文檔這是一個(gè)文檔指定覆蓋默認(rèn)的域名啟用 介紹 NoSQL:not only SQL,非關(guān)系型數(shù)據(jù) MongoDB是文檔型數(shù)據(jù),文檔是獨(dú)立的實(shí)體,文檔數(shù)據(jù)庫(kù)不適用于關(guān)聯(lián)關(guān)系明顯的數(shù)據(jù) S...
摘要:介紹非關(guān)系型數(shù)據(jù)是文檔型數(shù)據(jù),文檔是獨(dú)立的實(shí)體,文檔數(shù)據(jù)庫(kù)不適用于關(guān)聯(lián)關(guān)系明顯的數(shù)據(jù)提供了三種方式在應(yīng)用中使用通過(guò)注解實(shí)現(xiàn)對(duì)象文檔映射使用實(shí)現(xiàn)基于模板的數(shù)據(jù)庫(kù)訪問(wèn)自動(dòng)化的運(yùn)行時(shí)生成功能注解將類型映射為文檔這是一個(gè)文檔指定覆蓋默認(rèn)的域名啟用 介紹 NoSQL:not only SQL,非關(guān)系型數(shù)據(jù) MongoDB是文檔型數(shù)據(jù),文檔是獨(dú)立的實(shí)體,文檔數(shù)據(jù)庫(kù)不適用于關(guān)聯(lián)關(guān)系明顯的數(shù)據(jù) S...
摘要:聲明構(gòu)造函數(shù),作用是把從數(shù)據(jù)庫(kù)取出的數(shù)據(jù)實(shí)例化為對(duì)象。該構(gòu)造函數(shù)傳入的值為從中取出的數(shù)據(jù)省略接口提供增刪改查接口實(shí)現(xiàn)提供增刪改查接口實(shí)現(xiàn)提供了一個(gè)類似于的設(shè)計(jì)的類。 本文快速入門,MongoDB 結(jié)合SpringBoot starter-data-mongodb 進(jìn)行增刪改查 1、什么是MongoDB ? MongoDB 是由C++語(yǔ)言編寫的,是一個(gè)基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng)。...
摘要:踩到許多坑,記錄下一些基于的東西吧首先。王大錘那么查詢的時(shí)候,如果要根據(jù)查詢班級(jí)怎么辦,的查詢也非常簡(jiǎn)單。詳情可以查看官方文檔用法 剛接觸mongodb不久。踩到許多坑,記錄下一些基于spring-data-mongodb的東西吧 首先。應(yīng)該了解下什么情況下使用mongodb,什么情況下用mysql: 業(yè)務(wù)需要事物,使用mysql,因?yàn)閙ongodb不支持事物 數(shù)據(jù)量大,但是數(shù)據(jù)本身...
摘要:踩到許多坑,記錄下一些基于的東西吧首先。王大錘那么查詢的時(shí)候,如果要根據(jù)查詢班級(jí)怎么辦,的查詢也非常簡(jiǎn)單。詳情可以查看官方文檔用法 剛接觸mongodb不久。踩到許多坑,記錄下一些基于spring-data-mongodb的東西吧 首先。應(yīng)該了解下什么情況下使用mongodb,什么情況下用mysql: 業(yè)務(wù)需要事物,使用mysql,因?yàn)閙ongodb不支持事物 數(shù)據(jù)量大,但是數(shù)據(jù)本身...
閱讀 801·2021-10-09 09:44
閱讀 705·2019-08-30 13:55
閱讀 3165·2019-08-29 15:07
閱讀 3231·2019-08-29 13:09
閱讀 2422·2019-08-29 11:10
閱讀 1301·2019-08-26 14:05
閱讀 3606·2019-08-26 13:57
閱讀 2216·2019-08-23 16:42