Java mongodb System.js 應用

來源:互聯網
上載者:User

標籤:mongodb

近期遇到了一個比較麻煩的查詢,就想用mongodb system.js實現,有點類似關係型資料庫的預存程序的味道。

system.js是每個資料庫都會有的一個特殊集合,用來存放js的變數,可以在db.eval,mapreduce,where多個地方全域調用,比較方便。

下面是mongodb指令碼和java 調用部分代碼:

db.system.js.remove({_id:"calculateIdleRoomByTime"});// 這個可以將正常的會議室查詢出來,但是自己實現Java Bson to JavaBean的部分,比較麻煩db.system.js.<strong><span style="color:#009900;">insert</span></strong>({_id:"calculateIdleRoomByTime",value:function(startTime,endTime){   var roomIds = [],        rooms = [];        // 根據時間將被佔用的會議室編號查詢出來    var cursor =  db.conference.find({isDel:false,$or:[            {beginTime:{$gte:startTime},endTime:{$lte:endTime}},            {$or:[                    // 開始時間不在區間內                    {beginTime:{$lt:startTime},endTime:{$gt:startTime}},                    // 結束時間不在區間內                    {beginTime:{$lt:endTime},endTime:{$gt:endTime}}                ]            }        ]});    while(cursor.hasNext()){        var _conference =  cursor.next();        if(_conference.location){            roomIds.push(_conference.location.$id);        }    }    // read idle rooms    cursor = db.conferenceRoom.find({_id:{$nin:roomIds}});    while(cursor.hasNext()){        rooms.push(cursor.next());    }    return rooms; }});// 正式環境使用的是這個,查詢被佔用的會議室編號。然後Java Driver 使用$nin查詢出正常的會議室db.system.js.insert({_id:"calculateNotIdleRoomIdsByTime",value:function(startTime,endTime){   var roomIds = [],        rooms = [];        // 根據時間將被佔用的會議室編號查詢出來    var cursor =  db.conference.find({isDel:false,$or:[            {beginTime:{$gte:startTime},endTime:{$lte:endTime}},            {$or:[                    // 開始時間不在區間內                    {beginTime:{$lt:startTime},endTime:{$gt:startTime}},                    // 結束時間不在區間內                    {beginTime:{$lt:endTime},endTime:{$gt:endTime}}                ]            }        ]});    while(cursor.hasNext()){        var _conference =  cursor.next();        if(_conference.location){            roomIds.push(_conference.location.$id);        }    }    return roomIds; }});db.eval("calculateIdleRoomByTime(8,9)"); db.eval("calculateNotIdleRoomIdsByTime(2,14)"); // 簡單例子db.system.js.insert({_id:"max",value:function(a,b){return a>b?a:b;}});db.eval("max(2,399)");// 399

Java調用:

/** * 根據時間查詢空閑會議室 * @author Bill * @since V.10 2015年4月21日 - 上午10:03:57 * @param startTime * @param endTime * @return */@SuppressWarnings("unchecked")public List<ConferenceRoom> findByTime(long startTime, long endTime) {<span style="color:#009900;">Object result = mongoTemplate.getDb().eval("calculateNotIdleRoomIdsByTime("+startTime+","+endTime+")");</span>DBObject callResult = (DBObject) JSON.parse(JSON.serialize(result));Set<Entry<String, ObjectId>> notIdleRoomIdSets = callResult.toMap().entrySet();List<ObjectId> notIdleRoomIds = new ArrayList<ObjectId>();for (Entry<String, ObjectId> entry : notIdleRoomIdSets) {notIdleRoomIds.add(entry.getValue());}BasicQuery bq = new BasicQuery(new BasicDBObject("isDel", false).append("_id", new BasicDBObject("$nin", notIdleRoomIds)));return find(bq);}

挺有意思 。 

Java mongodb System.js 應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.