Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript mongodb每10分钟运行一个函数_Javascript_Mongodb_Mongoose - Fatal编程技术网

Javascript mongodb每10分钟运行一个函数

Javascript mongodb每10分钟运行一个函数,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,我在mongodb中有一份文件,如下所示: { id:1, requestType: { "api1": { count:1, firstAttemptTime: 1514360898751.0 }, "api2": { count:4, firstAttemptTime: 1514366897751.0 }

我在mongodb中有一份文件,如下所示:

{
    id:1,
    requestType: {
        "api1": { 
            count:1,
            firstAttemptTime: 1514360898751.0
        },
        "api2": { 
            count:4,
            firstAttemptTime: 1514366897751.0
        }
    }
}
{
    id:1,
    requestType: {
        "api2": { 
            count:4,
            firstAttemptTime: 1514366897751.0
        }
    }
}
我想在mongoose中编写一个函数,每10分钟调用一次,根据firstAttemptTime键从requestType中删除一个对象。例如,10分钟后,文件应如下所示:

{
    id:1,
    requestType: {
        "api1": { 
            count:1,
            firstAttemptTime: 1514360898751.0
        },
        "api2": { 
            count:4,
            firstAttemptTime: 1514366897751.0
        }
    }
}
{
    id:1,
    requestType: {
        "api2": { 
            count:4,
            firstAttemptTime: 1514366897751.0
        }
    }
}

似乎您想要删除基于时间戳的数据。在MongoDB中,可以通过为记录设置TTL来实现这一点,这样就不需要定期运行函数。您只需创建TTL索引并指定应该在多少秒后删除文档。有关于此的教程

您似乎希望根据时间戳删除数据。在MongoDB中,可以通过为记录设置TTL来实现这一点,这样就不需要定期运行函数。您只需创建TTL索引并指定应该在多少秒后删除文档。有关于这个的教程

类似的东西会有用

var mongoose = require('mongoose')
var db = mongoose.createConnection('mongodb://localhost:port/db')
var newSchema = new mongoose.Schema({
    id: Number,
    requestType: { type: {} },
    requestTypeList: { type: [] },
})
var model = db.model('newModel', newSchema)

setTimeout(function(){
    model.find({id: 1}, function(err, doc){
        // remove requestType properties first keyed value
        delete Object.keys(doc.requestType)[0]

        // or

        // remove requestType property's first value as an Array
        delete doc.requestTypeList[0] // I'd recomend because otherwise you'll be left with ever increasing key values like api90000000+ as the first key

        doc.save(function(err){
            if(!err){
                console.log('success :)')
            } else {
                console.log('oh oh')
            }
        })

    })
}, 600000) // 600000ms == 10 minutes

// es 5 or 6 idk..

setTimeout(()=>{
    model.find({id: 1}, (err, doc)=>{
        // remove requestType properties first keyed value
        delete Object.keys(doc.requestType)[0]

        // or

        // remove requestType property's first value as an Array
        delete doc.requestTypeList[0] // I'd recomend because otherwise you'll be left with ever increasing key values like api90000000+ as the first key

        doc.save((err)=>{
            if(!err){
                console.log('success :)')
            } else {
                console.log('oh oh')
            }
        })

    })
}, 600000) // 600000ms == 10 minutes

但我认为Priidik的TTL答案可能更可靠

像这样的方法会奏效

var mongoose = require('mongoose')
var db = mongoose.createConnection('mongodb://localhost:port/db')
var newSchema = new mongoose.Schema({
    id: Number,
    requestType: { type: {} },
    requestTypeList: { type: [] },
})
var model = db.model('newModel', newSchema)

setTimeout(function(){
    model.find({id: 1}, function(err, doc){
        // remove requestType properties first keyed value
        delete Object.keys(doc.requestType)[0]

        // or

        // remove requestType property's first value as an Array
        delete doc.requestTypeList[0] // I'd recomend because otherwise you'll be left with ever increasing key values like api90000000+ as the first key

        doc.save(function(err){
            if(!err){
                console.log('success :)')
            } else {
                console.log('oh oh')
            }
        })

    })
}, 600000) // 600000ms == 10 minutes

// es 5 or 6 idk..

setTimeout(()=>{
    model.find({id: 1}, (err, doc)=>{
        // remove requestType properties first keyed value
        delete Object.keys(doc.requestType)[0]

        // or

        // remove requestType property's first value as an Array
        delete doc.requestTypeList[0] // I'd recomend because otherwise you'll be left with ever increasing key values like api90000000+ as the first key

        doc.save((err)=>{
            if(!err){
                console.log('success :)')
            } else {
                console.log('oh oh')
            }
        })

    })
}, 600000) // 600000ms == 10 minutes

但我认为Priidik的TTL答案可能更可靠

我不想删除该文档。我只想删除一个对象的值。从这个示例中,我可以看到id字段也可以位于api1和api2文档中,然后您可以使用这个TTL过期。也许您提供的示例不是您正在使用的完整文档,但我认为您应该尝试创建您的文档和架构,以便在MongoDB中使用TTL expiry和not long running函数。我不想删除该文档。我只想删除一个对象的值。从这个示例中,我可以看到id字段也可以位于api1和api2文档中,然后您可以使用这个TTL过期。也许您提供的示例不是您正在使用的完整文档,但我认为您应该尝试创建您的文档和模式,以便它可以在MongoDB中使用TTL expiry和not long running函数。