Recently encountered a more troublesome query, just want to use MongoDB system.js implementation, a bit like the relational database of the stored procedure flavor.
System.js is a special set of each database , used to store JS variables, can be db.eval,mapreduce,where in many places global call, more convenient.
Here is a section of the code for MONGODB scripts and Java calls:
Db.system.js.remove ({_id: "Calculateidleroombytime"});//This can be a normal meeting room query out, but the implementation of Java Bson to JavaBean part, More trouble Db.system.js.<strong><span style= "color: #009900;" >insert</span></strong> ({_id: "Calculateidleroombytime", Value:function (starttime,endtime) {var Roomids = [], rooms = []; Based on the time will be occupied by the conference room number query out var cursor = Db.conference.find ({isdel:false, $or: [{begintime:{$gte: Starttime},endtim e:{$lte: EndTime}}, {$or: [//start time not within interval {begintime:{$lt: starttime},endtime : {$gt: StartTime}},//end time not within interval {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; }});//The formal environment uses this, querying the occupied conference room number. The Java Driver then uses $nin to query out the normal meeting room Db.system.js.insert ({_id: "Calculatenotidleroomidsbytime", Value:function (StartTime, EndTime) {var roomids = [], rooms = []; Based on the time will be occupied by the conference room number query out var cursor = Db.conference.find ({isdel:false, $or: [{begintime:{$gte: Starttime},endtim e:{$lte: EndTime}}, {$or: [//start time not within interval {begintime:{$lt: starttime},endtime : {$gt: StartTime}},//end time not within interval {begintime:{$lt: endtime},endtime:{$gt: EndTime}} ] } ]}); while (Cursor.hasnext ()) {var _conference = Cursor.next (); if (_conference.location) {Roomids.push (_conference.location. $id); }} return roomids; }});d B.eval ("Calculateidleroombytime (8,9)"); Db.eval ("Calculatenotidleroomidsbytime (2,14)"); Simple example Db.system.js.insert ({_id: "Max", Value:function (A, b) {return a> b?a:b;}}); Db.eval ("Max (2,399)");//399
Java calls:
/** * Check Availability room According to TIME * @author Bill * @since v.10 April 21, 2015-Morning 10:03:57 * @param startTime * @param endTime * @return * * @Su Ppresswarnings ("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);}
Very interesting.
Java MongoDB system.js Application