English 中文(简体)
删除文件,同时至少保存一个文件
原标题:Removing documents while preserving at least one

我拥有一个包含历史数据的MongoDB收集器,有id和时间。

I want to delete data from the collection older than a specific timestamp. But for every id at least one document (the newest) must stay in the collection.

附录一

{"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

... and I want to delete documents having a timestamp older than 2011-09-07 then 1 and 2 should not be deleted because they are newer. 4 should be deleted because it is older, but 3 should not be deleted (although it is older) because at least one document per id should stay in the collection.

Does anyone know how I can do this with casbah and/or on the mongo console?

Regards, Christian

最佳回答

我可以考虑几种方法。 第一,尝试:

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 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 };
};

将上述档案输入壳体:load(”file.js”;

然后管理:db.testdata.mapReduce (map, reduction,{out: “results”};

然后删除图Reduce输出:db.results.drop()。

问题回答

暂无回答




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签