Meteor Collections that expire

前端 未结 2 1295
无人及你
无人及你 2021-01-21 10:12

I would like to set a session bound value that\'s synced between client and server in Meteor. I\'m thinking that should be done in a Collection, since Session isn\'t synced betw

相关标签:
2条回答
  • 2021-01-21 10:57

    I would recommend indexing the particular field in the collection with a TTL parameter, this will ensure that documents expire after a specified time.

    Do it from the Mongo shell like so:

    db.myCollection.ensureIndex( { "fieldName": 1 }, { expireAfterSeconds: 3600 } )
    
    0 讨论(0)
  • 2021-01-21 11:04

    You could remove the records for a collection after a certain amount of time.

    Server side code:

    //Run every minute
    Meteor.setInterval(function() {
    
        MyCollection.remove({expires: { $gte: new Date() }});
    
    }, 60000);
    

    When you insert it you can set an expiry:

    Server side code:

    MyCollection.deny({
        insert: function(userId, doc) {
            //Set expiry to 24 hours from now
            doc.expires = new Date( new Date().getTime() + (36000000*24) );
    
        }
    });
    

    The deny method adds the expiry time on the server, even if you add a document from the client. This way you can ensure documents expire regardless of the time on the client, which can often be incorrect.

    Then you can insert the document on the client side

    Client side

    MyCollection.insert({ base64data: xxxxxx });
    

    Also yes you're right, Session does not sync with the server.

    0 讨论(0)
提交回复
热议问题