mongo 命令记录

//连接命令
mongo --host launcher-default-test.mongodb.singapore.rds.aliyuncs.com:3717



db.userHistory.find({},{"_id":1})



db.resource.insert({

    'resourceId':12123,'resourceType':'theme',

    'downloadHistory':[{'userId':1233,'downloadCountry':233,'downloadTime':1234555},{'userId':12334,'downloadCountry':2334,'downloadTime':12345554}],

    'praiseHistory':[{'userId':12333, 'praiseCountry':344, 'praiseIp':'192.168.12.11','praiseTime':14444545},{'userId':123333, 'praiseCountry':344, 'praiseIp':'192.168.12.11','praiseTime':14444545}],

    'trailHistory':[{'userId':12333, 'praiseCountry':344, 'praiseTime':14444545},{'userId':123333, 'praiseCountry':344, 'praiseTime':14444545}]

})




db.getCollection("userHistory").find({"downloadHistories.type": "theme", })

db.getCollection("userHistory").find({"downloadHistories.type": "theme"},{"downloadHistories.type":1, "downloadHistories.downloadTime":1})



//查询并且分组统计
db.userHistory.aggregate( [

    {$project:{"downloadHistories.type":1, "downloadHistories.downloadTime":1}},

    { $match: { "downloadHistories.type": "theme"} },

    {$group:{_id: "$downloadHistories.downloadTime",total: {$sum:1}}}

  ] );



  db.userHistory.aggregate( [

    {$project:{"downloadHistories.type":1, "downloadHistories.downloadTime":1}},

    {$unwind: "$downloadHistories"},

    { $match: { "downloadHistories.type": "theme"} },

    {$group:{_id: {dt:"$downloadHistories.downloadTime"} ,total: {$sum:1}}}

  ] );



  db.userHistory.aggregate( [

    { $match: { "downloadHistories.type": "theme"} },

    {$project:{"downloadHistories.type":1, "downloadHistories.downloadTime":1}},

    {$unwind: "$downloadHistories"},

    {$group:{_id:"$downloadHistories.type", total:{$sum:1}}}

  ] );



  db.userHistory.aggregate( [

    {$unwind: "$downloadHistories"},

    {$group:{_id:"$downloadHistories.type", total:{$sum:1}}},

  ] );



  db.userHistory.aggregate( [

    { $match: { "downloadHistories.type": {$ne:null}} },

    {$unwind: "$downloadHistories"},

    {$group:{_id:"$downloadHistories.type", total:{$sum:1}}},

  ] );



  db.userHistory.aggregate( [

    { $match: { "downloadHistories.type": {$ne:null}} },

    {$unwind: "$downloadHistories"}

  ] );




  db.userHistory.aggregate( [

    { $match: { "downloadHistories.ip": {$ne:null}} },

    {$unwind: "$downloadHistories"}

  ] );




  db.resourceLog.aggregate([

    {$match: {"_id":{$in:[NumberLong(126368877)]}}},

    {$unwind: "$downloadHistories"},

    {$match: {"downloadHistories.country": NumberInt(223)}},

    {$group:{"_id": "$_id", dlcount: {$sum: 1}}}

  ])



db.userHistory.aggregate([

    {$unwind: "$downloadHistories"},

    {$match: {"downloadHistories.downloadTime":  { "$gte" : "1553011200000", "$lte" : "1553097600000"}}},

    {$group : { "_id" : "$_id", "total" : { "$sum" : 1}}}

]);



db.resourceLog.aggregate([{ "$match" : { "id" : { "$in" : [109, 108, 107, 106, 105, 104, 102, 101, 99, 98]}}},

{ "$unwind" : "$downloadHistories"}, { "$match" : { "$and" : [{ "downloadHistories.country" : 639}]}}, { "$group" : { "_id" : "$id", "total" : { "$sum" : 1}}},

{ "$sort" : { "total" : -1}}]);



db.resourceLog.find({_id: 100});

db.resourceLog.update({_id: 100, "downloadHistories.downloadId": 100}, {$set:{"downloadHistories.$.country":NumberInt(222)}}, {multi: true});

db.resourceLog.update({_id: 100, "downloadHistories.downloadId": 100}, {$set:{"downloadHistories.$[].country":NumberInt(222)}}, {multi: true});



  db.resourceLog.save({"_id" : NumberLong(101),

  "downloadHistories" : [],

  "praiseHistories":[ ]

});

// 根据一个字段来更新另外一个字段
db.resourceDownloadLog.find({"time":{$exists: false}}).forEach(function(e){db.resourceDownloadLog.update({"_id": e._id}, {"$set":{"time": NumberLong(e.downloadTime)}})});
db.resourcePraiseLog.find().forEach(function(e){db.resourcePraiseLog.update({"_id": e._id}, {"$set":{"time": NumberLong(e.praiseTime)}})});
db.resourceTrailLog.find().forEach(function(e){db.resourceTrailLog.update({"_id": e._id}, {"$set":{"time": NumberLong(e.startTime)}})});

//我觉得应该还有更好的,不应该只是foreach

















 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值