这是本文档旧的修订版!


MongoDB 常用

用户管理

添加用户

use admin
db.createUser({user:'admin',pwd:'123456',roles:[{role:'userAdminAnyDatabase',db:'admin'}]})

冗余数据

查找冗余数据

db.HistoryData.aggregate([{
   '$group': {
        '_id': {'symbol': '$symbol','time': '$time'},
        'uniqueIds': {'$addToSet': '$_id'},
        'count': {'$sum': 1}}
    },{
        '$match': {'count': {'$gt': 1}
    }
}]).forEach(function(it){
     it.uniqueIds.shift();
        db.DataH.remove({_id: {$in: it.uniqueIds}});
}); //后面部分是删除冗余的数据

聚合

统计所有股票,总共有多少记录,最久的一个记录发生在哪天.

var match:any = [
{ $match :  {symbol: symbol}},
{
    $group:{
        _id: symbol,
        count: { $sum: 1 },
        min: { $min: '$time'}
    }
}]
var data: any = await this.db.Aggregate('Stock', 'DataD', match)

獲取所有symbol的最後一個日期的收盤價

db.DataD.aggregate( [ { $group : { _id : "$symbol" , close:{$first:'$close'}, time:{$first:'$time'}}} ] )

設定一個時間範圍

db.stock_us.aggregate( [ 
    { $group : { _id : "$symbol" , close:{$first:'$close'}, time:{$first:'$time'}}},
    {$match: {$and: [{time: {$lte: new Date('2021-09-18')}}, {time: {$gte: new Date('2021-09-15')}}]}}
] ).limit(30)
it/database/mongodb_command.1634699107.txt.gz · 最后更改: 2021-10-20 03:05 由 goldentianya
回到顶部
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0