差别

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

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
it:database:mongodb_command [2021-10-20 02:41] goldentianyait:database:mongodb_command [2024-12-15 07:56] (当前版本) – [正则表达式] goldentianya
行 1: 行 1:
 ====== MongoDB 常用 ====== ====== MongoDB 常用 ======
 +
 +===== 命令 =====
 +**ubuntu**
 +  - systemctl start mongod
 +  - systemctl status mongod
 +  - /etc/mongod.conf
 +
 +
 +**mac**
 +  - brew services start [email protected]
 +  - brew services stop [email protected]
 +  - mongod --config /opt/homebrew/etc/mongod.conf --fork
 +  - brew services list
 +  - /opt/homebrew/etc/mongod.conf
 +
 +===== 用户管理 =====
  
 添加用户 添加用户
行 7: 行 23:
 </code> </code>
  
 +===== 冗余数据 =====
 查找冗余数据 查找冗余数据
 <code bash> <code bash>
行 23: 行 40:
 </code> </code>
  
-统计所有股票,总共有多少记录,最的一个记录发生在哪天.+删除记录:删除3月30日之前的数据 
 + 
 +<code | download> 
 +db.Data30m.deleteMany({time: {$lt: new Date('2022-03-30')}}) 
 +</code> 
 + 
 +===== 聚合 ===== 
 + 
 +统计所有股票,总共有多少记录,最的一个记录发生在哪天.
 <code bash> <code bash>
 var match:any = [ var match:any = [
-{ $match :  {symbol: symbol}}, + { $match :  {symbol: symbol}}, 
-{ + { $group:{_id: symbol, count: { $sum: 1 }, min: { $min: '$time'}}
-    $group:{ +
-        _id: symbol, +
-        count: { $sum: 1 }, +
-        min: { $min: '$time'} +
-    }+
 }] }]
 var data: any = await this.db.Aggregate('Stock', 'DataD', match) var data: any = await this.db.Aggregate('Stock', 'DataD', match)
 </code> </code>
  
-獲取所有symbol的最後一個日期的收盤價+獲取所有symbol的最後一個日期的收盤價 (<color #7092be>表格已經按時間倒序設定了索引</color>
 <code> <code>
 db.DataD.aggregate( [ { $group : { _id : "$symbol" , close:{$first:'$close'}, time:{$first:'$time'}}} ] ) 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>
 +
 +===== 正则表达式 =====
 +
 +实现模糊查找,字段。
 +
 +參考((a>[[https://www.runoob.com/mongodb/mongodb-regular-expression.html|MongoDB 正则表达式]])) 具體實例
 +<code>
 +>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结束的
 +</code>
 +
 +<code >
 +db.stocks.find({$and: [{name: /3x/}, {name: /Trust/}]}) // 同时含有 ”3x“ 和 ”Trust“ 字符串的
 +db.stocks.find({name: {$in: [/3x/, /Trust/]}})          // 含有 ”3x“ 或者 ”Trust“ 字符串的
 +</code>
 +
 +===== 其它查询 =====
 +
 +列举不同的证券代号
 +<code>
 +db.DataD.distinct("symbol", { symbol: /DTG/ })
 </code> </code>
  
  
 {{tag>mongo database}} {{tag>mongo database}}
it/database/mongodb_command.1634697719.txt.gz · 最后更改: 2021-10-20 02:41 由 goldentianya
回到顶部
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0