差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
it:database:mongodb_command [2021-10-20 03:06] – [聚合] goldentianyait:database:mongodb_command [2025-12-30 15:01] (当前版本) – 移除 goldentianya
行 1: 行 1:
-====== MongoDB 常用 ====== 
  
-===== 用户管理 ===== 
- 
-添加用户 
-<code bash> 
-use admin 
-db.createUser({user:'admin',pwd:'123456',roles:[{role:'userAdminAnyDatabase',db:'admin'}]}) 
-</code> 
- 
-===== 冗余数据 ===== 
-查找冗余数据 
-<code bash> 
-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}}); 
-}); //后面部分是删除冗余的数据 
-</code> 
- 
-===== 聚合 ===== 
- 
-统计所有股票,总共有多少记录,最久的一个记录发生在哪天. 
-<code bash> 
-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) 
-</code> 
- 
-獲取所有symbol的最後一個日期的收盤價 
-<code> 
-db.DataD.aggregate( [ { $group : { _id : "$symbol" , close:{$first:'$close'}, time:{$first:'$time'}}} ] ) 
-</code> 
-設定一個時間範圍 
-<code> 
-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) 
-</code> 
- 
-{{tag>mongo database}} 
it/database/mongodb_command.1634699190.txt.gz · 最后更改: 2021-10-20 03:06 由 goldentianya
回到顶部
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0