2014년 7월 15일 화요일

mongoDB usable scripts

# get an average of collection with some conditions.
db.POINT_TOTAL_OBS_STATION_DATA.group(
   { cond: { obs_item_id : "OBSCD00074" }
   , initial: {count: 0, total:0}
   , reduce: function(doc, out) { out.count++ ; out.total += doc.v1 }
   , finalize: function(out) { out.avg = out.total / out.count }

} )

# improved a performance , however I don't know exactly why... may be hash !!
db.POINT_TOTAL_OBS_STATION_DATA.aggregate( [ 
     { $match: { obs_item_id : "OBSCD00074" } }, 
     { $group: { _id : 0 , v1_avg : { $avg: "$v1"} } } ] )


# group by each values
db.POINT_TOTAL_OBS_STATION_DATA.aggregate( [ 
     { $group: { _id : { key : "$obs_item_id" },  v1_avg : { $avg: "$v1"} } } ] )


# join query for a special case
db.POINT_TOTAL_OBS_STATION_DATA.aggregate( [{ $group: { _id : "$obs_item_id"} } , { $out : "fox_out" } ] )
fox = db.fox_out.find().toArray()
for ( var i = 0 ; i < fox.length ; i ++ ) {   db.fox_result.insert (db.OBS_ITEM_CODE.find( { obs_item_id : fox[i]._id }, { obs_item_id : 1, item_name_kor : 1 } ).toArray() ) }
db.fox_result.find().sort( { item_name_kor : 1 } )


# to find some for
db.POINT_TOTAL_OBS_STATION_DATA.aggregate( [
     { $match: { tm : { $gte : '2011-01-01 00:00:00', $lt : '2012-01-01 00:00:00' } }},
     { $group: { _id : 0 , v1_avg : { $avg: "$v1"} } } ] )


# Create index
db.POINT_TOTAL_OBS_STATION_DATA.ensureIndex( { obs_item_id : 1 } )
db.POINT_TOTAL_OBS_STATION_DATA.ensureIndex( { obs_time : 1 } )
.. more
db.system.indexes.find()

# how to check a elapsed time
db.setProfilingLevel(0) -- disable
db.setProfilingLevel(1) -- enable to 1 level
db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()


0 개의 댓글:

댓글 쓰기