mangreen / Some-Note

Development Memo
1 stars 0 forks source link

mongodb script #3

Open mangreen opened 9 years ago

mangreen commented 9 years ago

How to get the runchart of 1sec~60sec "to-store" & "e_trials - to-store"

db.rawlog.aggregate([{
    $match:{
        $or:[
            {"e": "time-clock"},
            {"e": "to-store"}
        ]
    }
},{
    $group:{
        _id:{
            sid: "$sid",
            cid: "$cid"
        },
        sec:{
            $sum:{
                $cond: [ { $eq: [ "$e", "time-clock"] } , 1, 0 ]
            }
        },
        store:{
            $sum:{
                $cond: [ { $eq: [ "$e", "to-store"] } , 1, 0 ]
            }
        }
    }
},{
    $match:{
        "store": {$gt: 0}
    }
},{
    $group:{
        _id: "$sec",
        sum:{ 
            $sum: 1
        }
    }
},{
    $sort:{
        _id: 1
    }
}
])
db.rawlog.aggregate([{
    $match:{
        $or:[
            {"e": "time-clock"},
            {"e": "to-store"},
            {"d_label": "vm5_player"}
        ]
    }
},{
    $group:{
        _id:{
            sid: "$sid",
            cid: "$cid"
        },
        sec:{
            $sum:{
                $cond: [ { $eq: [ "$e", "time-clock"] } , 1, 0 ]
            }
        },
        store:{
            $sum:{
                $cond: [ { $eq: [ "$e", "to-store"] } , 1, 0 ]
            }
        },
        player:{
            $sum:{
                $cond: [ { $eq: [ "$d_label", "vm5_player"] } , 1, 0 ]
            }
        }
    }
},{
    $match:{
        "store": {$lt: 1},
        "player": {$gt:0}
    }
},{
    $group:{
        _id: "$sec",
        sum:{ 
            $sum: 1
        }
    }
},{
    $sort:{
        _id: 1
    }
}
])

How to quick insert val to all

db.book.update(
   { _id : { $exists : true } },
   {
       $set: {
            verified: true
        }
   },
   {multi: true }
)

How to use $sum with $eq

db.book.aggregate([
{
    $group: {
        _id: "releaseDate",
        sum: {
            $sum: { $cond: [ { $eq: [ "$pubId", "OA"] } , 1, 0 ] }
        },
        sum2: {
            $sum: { $cond: [ { $eq: [ "$listPrice", 44] } , 1, 0 ] }
            }
    }
},{ 
    $project : { 
        _id: 0, 
        sum : {
            $divide: ["$sum", "$sum2"]
        } 
    } 
}
])

How to deal with the timezone issue when storing dates in utc using mongod?

http://stackoverflow.com/questions/18287493/how-to-deal-with-the-timezone-issue-when-storing-dates-in-utc-using-mongod

db.book.aggregate([{
    $match : {"releaseDate": {$exists: true}}
},{
    $project : { 
        localtime : { 
            $subtract : [ "$releaseDate", 8*60*60*1000] 
        } 
    }
},{
    $group: {
        _id: {
            year: {$year: "$localtime"},
            month: {$month: "$localtime"},
            day: {$dayOfMonth: "$localtime"}
        },
        sum: {
            $sum: 1
        }
    }    
}])

How to filter array in subdocument with MongoDB

http://docs.mongodb.org/manual/reference/operator/aggregation/group/ http://stackoverflow.com/questions/15117030/how-to-filter-array-in-subdocument-with-mongodb

db.book.aggregate([
    { $group : { _id : "$pubId", pName: { $push: "$publisherName" }, price: { $push: "$listPrice" }} }
    ,{ $unwind: '$price'}
    ,{ $match: {'price': 44}}
])
{
    "result" : [ 
        {
            "_id" : "OA",
            "pName" : [ 
                "O&A"
            ],
            "price" : 44
        }, 
        {
            "_id" : "OA",
            "pName" : [ 
                "O&A"
            ],
            "price" : 44
        }
    ],
    "ok" : 1
}