mongodb - mongo聚合查詢使用$out輸出,執行的是取代操作而不是append操作
PHPz
PHPz 2017-05-02 09:18:44
0
1
790

mongo聚合查詢遇到的一個問題

目前對mongo資料庫的一張表做一些聚合查詢的操作,考慮到資料量太大,從資料庫查尋出來結果還是需要插入到資料庫,故決定,將查詢結果插入到一個臨時的集合中,使用mongo的$out操作。根據業務需求,需要執行三次這樣的聚合查詢,測試發現$out輸出到新的集合是,執行替換操作,而不是append操作,以下是官網解釋:

  • The $out operation creates a new collection in the current database if one does not already exist.

  • If the collection specified by the $out operation already exists, then upon completion of the aggregation, the $out stage atomically replaces the existing collection with the new results collection. The $outnotation doindex collection.

現在我仍然有這樣的需求,將多次聚合的結果放在一張表中,該怎麼做?

PHPz
PHPz

学习是最好的投资!

全部回覆(1)
滿天的星座

從2.6開始aggregation的回傳值是一個遊標。既然是遊標,就不要求你在客戶端把整個結果集緩存到內存中再操作,很直接的想法就是遍歷這個遊標,一條一條插進目標集合就可以了。例如在shell:

db.coll.aggregate([...]).forEach(function(doc) {
  db.target_coll.save(doc);
});
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!