MongoDB,更新和控制文档过期

时间:2016-03-08 06:02:37

标签: node.js mongodb ttl

我正在开发node.js项目。我试图了解MongoDB的工作原理。我通过cron文件每小时获取一次数据。我希望有独特的数据,所以我使用的是更新而不是插入。这很好。我想在三天后添加数据过期的选项。我不清楚如何做到这一点。

在伪代码中:

设置Vars,URL&#,几个全局变量,lineNr = 1,end_index =#包括databaseUrl。

   MongoClient.connect(databaseUrl, function(err, db) {
    assert.equal(null, err, "Database Connection Troubles: " + err);
 **** db.collection('XYZ_Collection').createIndex({"createdAt": 1},
            {expireAfterSeconds: 120}, function() {});   **** (update)

   s = fs.createReadStream(text_file_directory + 'master_index.txt')
        .pipe(es.split())
        .pipe(es.mapSync(function(line) { 
                s.pause();  // pause the readstream
                lineNr += 1;
                getContentFunction(line, s); 
                if (lineNr > end_index) {
                    s.end();
                }
        })
        .on('error', function() {
            console.log('Error while reading file.');
        })
        .on('end', function() {
            console.log('All done!');
        })
    );

    function getContentFunction(line, stream){ 
        (get content, format it, store it as flat JSON CleanedUpContent)
        var go = InsertContentToDB(db, CleanedUpContent, function() {
            stream.resume();
        });
    }  

    function InsertContentToDB(db, data, callback)   
            (expiration TTL code if placed here generates errors too..)             
            db.collection('XYZ_collection').update({
                    'ABC': data.abc,
                    'DEF': data.def)
                }, {
                    "createdAt": new Date(),
                    'ABC': data.abc,
                    'DEF': data.def,
                    'Content': data.blah_blah
                }, {
                    upsert: true
                },
                function(err, results) {
                    assert.equal(null, err, "MongoDB Troubles: " + err);
                    callback();
                });
    }

因此,带有两个字段的db.collection('')。update()形成一个复合索引,以确保数据是唯一的。 upsert = true允许适当地插入或更新。我的数据差别很大。某些内容是唯一的,其他内容是先前提交的更新。我想我有这个独特的插入或更新功能正常工作。 Info from...here

我真正想要添加的是对集合中文档的自动过期。我看到lots of content,但我对如何实现它感到茫然。

如果我尝试

db.collection('XYZ_collection')
  .ensureIndex( { "createdAt": 1 }, 
                { expireAfterSeconds: 259200 } );   // three days 

错误

  

/opt/rh/nodejs010/root/usr/lib/node_modules/mongodb/lib/mongodb/mongo_client.js:390                                  扔错了                                        ^                    错误:如果没有提供的回调,则无法使用writeConcern                        在Db.ensureIndex(/opt/rh/nodejs010/root/usr/lib/node_modules/mongodb/lib/mongodb/db.js:1237:11)                        在Collection.ensureIndex(/opt/rh/nodejs010/root/usr/lib/node_modules/mongodb/lib/mongodb/collection.js:1037:11)                        在tempPrice(/var/lib/openshift/56d567467628e1717b000023/app-root/runtime/repo/get_options_prices.js:57:37)                        at /opt/rh/nodejs010/root/usr/lib/node_modules/mongodb/lib/mongodb/mongo_client.js:387:15                        at process._tickCallback(node.js:442:13)

如果我尝试使用createIndex,我会收到此错误...

`TypeError: Cannot call method 'createIndex' of undefined`  

注意数据库完全是空的,通过db.XYZ_collection.drop()所以是的,我是Mongo的新手。谁知道我需要做什么?一个注意事项,我对something I read:非常困惑,如果索引字段已被另一个索引使用,则无法创建TTL索引。我想我没事,但我不清楚。

  

选择TTL索引有一些限制:您无法创建   如果索引字段已在另一个索引中使用,则为TTL索引。指数   不能有多个字段。索引字段应为Date bson类型

一如既往,非常感谢您的帮助。

更新:我已添加上面的createIndex代码。使用空回调,它运行时没有错误,但TTL系统根本无法删除条目,叹息。

0 个答案:

没有答案