//连接命令
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