这是本文档旧的修订版!
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: {time: {$lte: new Date('2021-09-18')}}}
] ).limit(30)
