日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

spring-data-mongodb与mongo shell的对应关系

發(fā)布時(shí)間:2025/3/20 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring-data-mongodb与mongo shell的对应关系 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

除了特殊注釋外,本文的測試結(jié)果均基于?spring-data-mongodb:1.10.6.RELEASE(spring-boot-starter:1.5.6.RELEASE),MongoDB 3.0.6


?  上一章我們了解了mongo shell中aggregate復(fù)雜的互相調(diào)用關(guān)系。那么,spring-data-mongodb中aggregate又是如何與mongo交互的?是將語句拼接成我們熟悉的函數(shù)(db._collection_.find/update...)還是命令(db.runCommand)?讓我們來看底層的代碼到底是怎么樣的。

  以下為 mongoTemplate.aggregate?方法的底層實(shí)現(xiàn)

private void sendMessage(final CommandMessage message, final InternalConnection connection) {ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);try {int documentPosition = message.encodeWithMetadata(bsonOutput).getFirstDocumentPosition();sendStartedEvent(connection, bsonOutput, message, documentPosition);connection.sendMessage(bsonOutput.getByteBuffers(), message.getId());} finally {bsonOutput.close();}}

?  在往下就是字節(jié)流了,就直接看著里的數(shù)據(jù)吧,message數(shù)據(jù)為:

?  test是連接的數(shù)據(jù)庫名稱,test.$cmd相當(dāng)于db.$cmd,有過第二章的基礎(chǔ),這里應(yīng)該明白,這個(gè)方法是走runCommand的$cmd集合的。那么相應(yīng)的?mongoTemplate.getDb().command?以及?mongoTemplate.getCollection("$cmd").findOne?都可以拼接出來。

  因?yàn)檫@些方法需要的拼接很復(fù)雜的bson,所以這里我們引用另外一個(gè)操作符$eval。熟悉js的朋友應(yīng)該都知道,eval是可以將字符串轉(zhuǎn)換成方法執(zhí)行的,這里我們就使用原生的aggregate語句字符串,讓mongo shell去處理字符串。

  如下是五種使用$eval的方法

//注意:命令的key:value部分,value必須被[]或者{}或者''包裹。js的字符串支持單引號和雙引號兩種,這里使用單引號是為了避免在java中使用大量的 \
     String command = "db.user.aggregate([{$group:{_id:'$name',count:{$sum:'$age'}}}])";BasicDBObject bson = new BasicDBObject();bson.put("$eval",command);Object object1 = mongoTemplate.getDb().doEval(command);Object object2 = mongoTemplate.getDb().command(bson);Object object3 = mongoTemplate.getCollection("$cmd").findOne(bson);ScriptOperations operations = mongoTemplate.scriptOps();ExecutableMongoScript script = new ExecutableMongoScript(command);Object object4 = operations.execute(script);/*** call是調(diào)用system.js集合中方法的方法,傳入?yún)?shù)是sysytem.js表中數(shù)據(jù)的主鍵值,* 可在mongo shell中天插入或者使用如下代碼插入。* 插入一次后可直接使用*/ // String command = "function(){return db.user.aggregate([{$group:{_id:'$name',count:{$sum:'$age'}}}])}"; // NamedMongoScript namedMongoScript = new NamedMongoScript("user2",script); // operations.register(namedMongoScript);Object object5 = operations.call("user2");

?  那么,find函數(shù)是否也如mongo shell 中,讓我們繼續(xù)來看看底層代碼

private <T> List<T> executeFindMultiInternal(CollectionCallback<DBCursor> collectionCallback, CursorPreparer preparer,DbObjectCallback<T> objectCallback, String collectionName) {try {DBCursor cursor = null;try {cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));if (preparer != null) {cursor = preparer.prepare(cursor);}List<T> result = new ArrayList<T>();while (cursor.hasNext()) {DBObject object = cursor.next();result.add(objectCallback.doWith(object));}return result;} finally {if (cursor != null) {cursor.close();}}} catch (RuntimeException e) {throw potentiallyConvertRuntimeException(e, exceptionTranslator);}}

  我們可以看到find也如mongo shell中一樣,走的是游標(biāo)的路線。

  通過上面的一系列代碼的研究,我們學(xué)會了使用多種方法實(shí)現(xiàn)原生的aggregate,并且弄明白了aggregate在mongo shell 或者spring-data-mongodb中都存在多層的、暴露的調(diào)用方法,而find類型的請求是直接調(diào)用到了游標(biāo),這樣設(shè)計(jì)的目的是什么?有興趣的讀者可以繼續(xù)看看第四章?mongo中的游標(biāo)與數(shù)據(jù)一致性的取舍。

 


?目錄

  一:spring-data-mongodb 使用原生aggregate語句

  二:mongo的runCommand與集合操作函數(shù)的關(guān)系

  三:spring-data-mongodb與mongo shell的對應(yīng)關(guān)系

  四:mongo中的游標(biāo)與數(shù)據(jù)一致性的取舍

轉(zhuǎn)載于:https://www.cnblogs.com/ttjsndx/p/9947370.html

總結(jié)

以上是生活随笔為你收集整理的spring-data-mongodb与mongo shell的对应关系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。