MONGDB Shell Scripting Learning

Source: Internet
Author: User
Tags getdate mongodb

Just started contacting the mongdb shell. Here is a record of the learning process.

Open the shell in the console.

Linux Environment:/usr/local/mongo/bin/mongo

Window Environment: D:\mongodb\mongodb-win32-i386-2.0.6\bin>mongo


The basic section can be consulted: http://www.cnblogs.com/anan/archive/2012/06/15/2550553.html


First create a JS function

function query () {
print ("Start execution--------------");
Query a table Video_encode
var encode_coll=db.video_encode.find ({status:{"$lt": Ten});
Print (Encode_coll.count ());//number of prints
//loop variable while
(Encode_coll.hasnext ()) {
var v=encode_coll.next ();
Print (v.name);//Printing Properties
} print
("End Execution--------------");
}

Call the query () method directly to execute. Figure below



This function is primarily used to query records from a table based on criteria, and then iterate through the records to print.

Note When you first start learning, you encounter such a question.

In getting a collection how do I know how to manipulate this set such as Var encode_coll=db.video_encode.find ();

How to know this object.

You can actually know his internal structure by traversing the object.

For example:

for (var param in encode_coll) {

print (param);


You will see the properties of the returned cursor as shown here:


Obviously this is the encapsulation of the MONGDB internal to the Db.find () return object, so we can do better.

Here's an example:

I have two tables: Video_uplaod (uploadid,size), Transcode_job (id,uploadid,act)

I want to count the size of act = "CLOUD" for all files.

The idea is to first isolate all uploadids (arrays) from the table transcode_job according to act = "CLOUD".

In accordance with Uploadids, all sizes are counted from the table video_upload.

As shown in figure:




Statistical transcoding failure Rate

 
function Fun_encode_fail () {//Statistical time period, transcoding failure rate var b_time=new date (2012,05,01);//2012-06-01 var e_time=new date (2012,06,01) ;//2012-02-01 var temp_time=new Date (2012,05,01);//Temporary Time B_time.setdate (B_time.getdate () +7);

Add seven days//cycle add date var weeks=0;
		Statistics for the specified time class, transcoding failure rate function Failrate (starttime,endtime) {print ("StartTime:" +starttime+ "EndTime:" +endtime); var count=0;//number of transcoding var retrytimes=0;//failures var fail_rate=0;//failure rate//query is the query condition, is the JSON object var query={"status": {"$in": [
		20,30,70,-100,-101,-200]}, "Createtime": {"$gt": StartTime, "$lte": EndTime}};
		var cusor_encode=db.video_encode.find (query);
var cusor_history=db.video_encode_history.find (query);
			  Loop Cusor_encode while (Cusor_encode.hasnext ()) {var video=cusor_encode.next ();
			  if (video.retrytimes>0) {retrytimes+=video.retrytimes;
			} count++;
			  }//Loop cusor_history while (Cusor_history.hasnext ()) {var video=cusor_history.next ();
			  if (video.retrytimes>0) {retrytimes+=video.retrytimes;} count++;
		} fail_rate=count+retrytimes==0?0:retrytimes/(Count+retrytimes);
		Print ("Number of transcoding:" +count);
		Print ("Number of failures:" +retrytimes);
		Print ("Failure rate:" +fail_rate); var fail_rate_json={"Week": Weeks, "startTime": StartTime, "EndTime": EndTime, "transcodenums": Count, "Failtimes":
		Retrytimes, "Failrate": fail_rate};
	Db.fail_rate_info.insert (Fail_rate_json);
	}//Initialization table Fail_rate_info Db.fail_rate_info.remove ();
		while (b_time<e_time) {print ("+ (++weeks) +" Week "); Failrate (Temp_time.gettime (), B_time.gettime ());
		The function needs to be declared in advance temp_time.setdate (Temp_time.getdate () +7);
	B_time.setdate (B_time.getdate () +7); }
}
 
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.