Java MongoDB system.js Application

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.