2014-10-28 65 views
0

我想備份Lunr指數蒙戈(每日),因爲它是圍繞13MB運行,我觸發MongoError:文檔比上限大小誤差較大。我想使用GridFS來解決這個問題,但我有一段時間讓它點擊。什麼是存儲使用GridFS的流星大對象最簡單的方法?

簡單來說:在流星,我想保存使用GridFS的一個13MB的JSON對象到MongoDB的,然後就可以在必要時進行檢索 - 所有隻在服務器上。

我已經通過文件收集和CollectionFS文檔不見了,他們似乎對我想要實現過於複雜,而且似乎沒有解決簡單存儲變量的內容。 (或者,更可能的是,他們這樣做,而我只是失蹤了。)

這是我想要做的,在僞代碼是什麼:

Backup = new GridFSCollection('backup'); 

var backupdata = (serialized search index data object); 
Backup.insert({name:'searchindex', data:backupdata, date:new Date().getTime()}); 

var retrieved = Backup.findOne({name:'searchindex'}); 

有什麼建議?

回答

0
var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db; 
var GridStore = MongoInternals.NpmModule.GridStore; 

var largedata = new GridStore(db,'name','w'); //to write 
largedata.open(function(error,gs){ 
    if (error) return; 
    gs.write('somebigdata',function(error,done){ 
     if (error) return; 
     largedata.close(); 
    }) 

}); 


var largedata = new GridStore(db,'name','r'); //to read data 
largedata.open(function(error,gs){ 
    if (error) return; 
    gs.read(function(error,result){ 
     if (error) return; 
     //then do something with the result 
     largedata.close(); 
    }) 

}); 

解釋

首先,你需要的數據庫對象,它可以通過MongoInternals暴露https://github.com/meteor/meteor/tree/devel/packages/mongo

其次,你需要的Gridstore的對象,你從MongoInternals得到它,以及

然後您可以創建一個寫入或讀取對象並按照API http://mongodb.github.io/node-mongodb-native/1.4/markdown-docs/gridfs.html

由流星使用的蒙戈是1.3.x的,而最新的節點MongoDB的本地驅動程序是2.x.x http://mongodb.github.io/node-mongodb-native/2.0/api-docs/

所以API可能在將來改變。目前,你需要做的開啓和關閉,並且回調都是異步,您可能需要使用Meteor.wrapAsync(可能無法在largedata.open工作),未來或光纖

來包裝它們
相關問題