2013年8月23日星期五

mongodb

 Before writing some mongodb

synchronous or operating procedures, often using perl, or even c implementation , so the procedure is very cumbersome and poor control logic , and even what some of the features and commands in these languages mongo driver not implemented . The shell was later found mongodb javascript to achieve, if the direct use js to achieve the corresponding functions would seem very intuitive and easy to use.

 

first in the js mongo commands can be used directly , but did not like the c bson like stitching with various statements. For example, in the js can write :

 

 
  
Date.prototype.format = function(format) 
{
var o =
{
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format))
format
=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(format))
format
= format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var myDate = new Date();
var datetime = myDate.format("yyyy-MM-dd hh:mm:ss");
print(datetime);

var cursor = db.my_soft_info.find({"percent":0,"starttime":{$lt: datetime},"endtime":{$gt: datetime}});
while(cursor.hasNext())
{
var temp = cursor.next();
print(tojson(temp.bookid));
var arr = db.soft_basic_info.findOne({"id":temp.uid},{softcount:1});
db.soft_basic_info.update({
"id":temp.uid},{$set:{"freedate":arr.softcount+1,"getgrade":3}});
}
 
 

can then be invoked directly in mongo js script , or shell scripts , if used in shell scripts js script , together with other regular tasks crontab tool , can be used as a scheduled task mongodb service program .

 

js script used in the shell can be written . It is convenient

 
  
mongo host:port/dbname --shell jsname.js;
 
 

 

 

没有评论:

发表评论