Scala 在保留至少一个文档的同时删除文档

Scala 在保留至少一个文档的同时删除文档,scala,mongodb,casbah,Scala,Mongodb,Casbah,我有一个MongoDB集合,其中包含id和时间戳的历史数据 我想从集合中删除比特定时间早的数据 时间戳。但每个id至少有一个 文档(最新的)必须保留在集合中 假设我的收藏中有以下文档 {"id" : "11", "timestamp" : ISODate("2011-09-09T10:27:34.785Z")} //1 {"id" : "11", "timestamp" : ISODate("2011-09-08T10:27:34.785Z")} //2 {"id" : "22", "time

我有一个MongoDB集合,其中包含id和时间戳的历史数据

我想从集合中删除比特定时间早的数据 时间戳。但每个id至少有一个 文档(最新的)必须保留在集合中

假设我的收藏中有以下文档

{"id" : "11", "timestamp" : ISODate("2011-09-09T10:27:34.785Z")} //1
{"id" : "11", "timestamp" : ISODate("2011-09-08T10:27:34.785Z")} //2

{"id" : "22", "timestamp" : ISODate("2011-09-05T10:27:34.785Z")} //3
{"id" : "22", "timestamp" : ISODate("2011-09-01T10:27:34.785Z")} //4
。。。我想删除时间戳早于 2011-09-07那么 不应删除1和2,因为它们较新。 应删除4,因为它较旧,但不应删除3 (虽然比较老)因为 每个id至少应在集合中保留一个文档

有人知道我如何在casbah和/或mongo上做到这一点吗 控制台

问候,,
克里斯汀

我可以想出几个办法。首先,试试这个:

var cutoff = new ISODate("2011-09-07T00:00:00.000Z");
db.testdata.find().forEach(function(data) {
    if (data.timestamp.valueOf() < cutoff.valueOf()) {
        // A candidate for deletion
        if (db.testdata.find({"id": data.id, "timestamp": { $gt: data.timestamp }}).count() > 0) {
            db.testdata.remove({"_id" : data._id});
         }
    }
});
var截止值=新的ISODate(“2011-09-07T00:00:00.000Z”);
db.testdata.find().forEach(函数(数据){
if(data.timestamp.valueOf()0){
remove({“\u id”:data.\u id});
}
}
});
这正是你想要的工作。或者您也可以使用MapReduce作业来完成。将其加载到文本文件中:

var map = function() {
    emit(this.id, {
        ref: this._id,
        timestamp: this.timestamp
    });
};


var reduce = function(key, values) {
    var cutoff = new ISODate("2011-09-07T00:00:00.000Z");
    var newest = null;
    var ref = null;
    var i;
    for (i = 0; i < values.length; ++i) {
        if (values[i].timestamp.valueOf() < cutoff.valueOf()) {
            // falls into the delete range
            if (ref == null) {
                ref = values[i].ref;
                newest = values[i].timestamp;
            } else if (values[i].timestamp.valueOf() > newest.valueOf()) {
                // This one is newer than the one we are currently saving.
                // delete ref
                db.testdata.remove({_id : ref});
                ref = values[i].ref;
                newest = values[i].timestamp;
            } else {
                // This one is older
                // delete values[i].ref
                db.testdata.remove({_id : values[i].ref});
            }
        } else if (ref == null) {
            ref = values[i].ref;
            newest = values[i].timestamp;
        }
    }
    return { ref: ref, timestamp: newest };
};
var map=function(){
发出(此.id{
参考号:这个,
timestamp:this.timestamp
});
};
var reduce=函数(键、值){
var截止值=新的等值日期(“2011-09-07T00:00:00.000Z”);
var=null;
var ref=null;
var i;
对于(i=0;ilatest.valueOf()){
//这个比我们当前保存的更新。
//删除参考
remove({u id:ref});
ref=值[i].ref;
最新=值[i]。时间戳;
}否则{
//这个比较老
//删除值[i].ref
remove({u id:values[i].ref});
}
}else if(ref==null){
ref=值[i].ref;
最新=值[i]。时间戳;
}
}
返回{ref:ref,时间戳:latest};
};
将上述文件加载到shell中:
Load(“file.js”)

然后运行它:
db.testdata.mapReduce(map,reduce,{out:“results”})


然后删除mapReduce输出:
db.results.drop()

我可以想出几种方法。首先,试试这个:

var cutoff = new ISODate("2011-09-07T00:00:00.000Z");
db.testdata.find().forEach(function(data) {
    if (data.timestamp.valueOf() < cutoff.valueOf()) {
        // A candidate for deletion
        if (db.testdata.find({"id": data.id, "timestamp": { $gt: data.timestamp }}).count() > 0) {
            db.testdata.remove({"_id" : data._id});
         }
    }
});
var截止值=新的ISODate(“2011-09-07T00:00:00.000Z”);
db.testdata.find().forEach(函数(数据){
if(data.timestamp.valueOf()0){
remove({“\u id”:data.\u id});
}
}
});
这正是你想要的工作。或者您也可以使用MapReduce作业来完成。将其加载到文本文件中:

var map = function() {
    emit(this.id, {
        ref: this._id,
        timestamp: this.timestamp
    });
};


var reduce = function(key, values) {
    var cutoff = new ISODate("2011-09-07T00:00:00.000Z");
    var newest = null;
    var ref = null;
    var i;
    for (i = 0; i < values.length; ++i) {
        if (values[i].timestamp.valueOf() < cutoff.valueOf()) {
            // falls into the delete range
            if (ref == null) {
                ref = values[i].ref;
                newest = values[i].timestamp;
            } else if (values[i].timestamp.valueOf() > newest.valueOf()) {
                // This one is newer than the one we are currently saving.
                // delete ref
                db.testdata.remove({_id : ref});
                ref = values[i].ref;
                newest = values[i].timestamp;
            } else {
                // This one is older
                // delete values[i].ref
                db.testdata.remove({_id : values[i].ref});
            }
        } else if (ref == null) {
            ref = values[i].ref;
            newest = values[i].timestamp;
        }
    }
    return { ref: ref, timestamp: newest };
};
var map=function(){
发出(此.id{
参考号:这个,
timestamp:this.timestamp
});
};
var reduce=函数(键、值){
var截止值=新的等值日期(“2011-09-07T00:00:00.000Z”);
var=null;
var ref=null;
var i;
对于(i=0;ilatest.valueOf()){
//这个比我们当前保存的更新。
//删除参考
remove({u id:ref});
ref=值[i].ref;
最新=值[i]。时间戳;
}否则{
//这个比较老
//删除值[i].ref
remove({u id:values[i].ref});
}
}else if(ref==null){
ref=值[i].ref;
最新=值[i]。时间戳;
}
}
返回{ref:ref,时间戳:latest};
};
将上述文件加载到shell中:
Load(“file.js”)

然后运行它:
db.testdata.mapReduce(map,reduce,{out:“results”})

然后删除mapReduce输出:
db.results.drop()