【问题标题】:What's the easiest way to store a large object in Meteor using GridFS?使用 GridFS 在 Meteor 中存储大对象的最简单方法是什么?
【发布时间】:2023-04-10 19:51:01
【问题描述】:

我正在尝试将 Lunr 索引备份到 Mongo(每天),因为它运行大约 13MB,我正在触发 MongoError: document is large than capped size 错误。我想使用 GridFS 来解决这个问题,但我很难让它点击。

用最简单的话来说:在 Meteor 中,我想使用 GridFS 将一个 13MB 的 JSON 对象保存到 MongoDB,然后能够在必要时检索它——所有这些都只在服务器上。

我已经浏览了 File-Collection 和 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'});

有什么建议吗?

【问题讨论】:

    标签: mongodb meteor gridfs


    【解决方案1】:
    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();
        })
    
    });
    

    解释

    首先您需要 db 对象,该对象可以通过 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

    Meteor 使用的 mongo 是 1.3.x 而最新的 node mongodb 原生驱动是 2.x.x http://mongodb.github.io/node-mongodb-native/2.0/api-docs/

    因此,API 将来可能会发生变化。目前,你需要做 open 和 close 并且回调都是异步的,你可能想用 Meteor.wrapAsync 包装它们(可能不适用于 largedata.open),Future 或 Fiber

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      相关资源
      最近更新 更多