ubuntu
mac
添加用户
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}}); }); //后面部分是删除冗余的数据
删除记录:删除3月30日之前的数据
download
db.Data30m.deleteMany({time: {$lt: new Date('2022-03-30')}})
统计所有股票,总共有多少记录,最早的一个记录发生在哪天.
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)
实现模糊查找,字段。
參考1) 具體實例
>db.posts.find({post_text:{$regex:"runoob"}}) >db.posts.find({post_text:/runoob/}) //包含runoob的 >db.posts.find({post_text:/^runoob/}) //以runoob開頭的 >db.posts.find({post_text:/runoob$/}) //以runoob结束的
db.stocks.find({$and: [{name: /3x/}, {name: /Trust/}]}) // 同时含有 ”3x“ 和 ”Trust“ 字符串的 db.stocks.find({name: {$in: [/3x/, /Trust/]}}) // 含有 ”3x“ 或者 ”Trust“ 字符串的
列举不同的证券代号
db.DataD.distinct("symbol", { symbol: /DTG/ })