MongoDB $ where queries and tail cursors are WAS: best practice regarding date

My problem: give me a list of documents older than X time.

If I have a document created with

db.dates.insert({date: new Date()});

And now I want to find it only when the “date” has become 30 minutes:

db.dates.find({ $where: "this.date.getTime() + 30 * 60000 <= new Date()"});

This works, but the Mongo docs say quite clearly that there is a significant performance hit for $ where the requests are.

So the question is, is there a better way?

========== UPDATE 1 ===========

I should add that I hope that this query function “dynamically” will create a query once and use it to get the tail cursor on a closed collection ... and I'm not sure that this is actually possible.

I will test and rewrite.

========== UPDATE 2 ===========

, , "" , , ", ", , ( db. CPP):

if ( replSettings.slavedelay && ( unsigned( time( 0 ) ) < nextOpTime.getSecs() + replSettings.slavedelay ) ) {
    assert( justOne );
    oplogReader.putBack( op );
    _sleepAdviceTime = nextOpTime.getSecs() + replSettings.slavedelay + 1;
    dblock lk;
    if ( n > 0 ) {
        syncedTo = last;
        save();
    }
    log() << "repl:   applied " << n << " operations" << endl;
    log() << "repl:   syncedTo: " << syncedTo.toStringLong() << endl;
    log() << "waiting until: " << _sleepAdviceTime << " to continue" << endl;
    return okResultCode;
}
+5
4

, . , :

, , .

:

  • .
  • , X .
  • X .
  • , .

, , - oplog:

if (cursor.hasNext()) {
    DBObject obj = cursor.next();

    if (config.getQueueDelay() > 0) {
        ObjectId objId = (ObjectId) obj.get("_id");
        long advisedSleep = (objId.getTime() + config.getQueueDelay() * 60000)
        - System.currentTimeMillis();
        if (advisedSleep > 0 ) {
            LOG.debug(
                "object is not yet old enough, sleeping for: " 
                 + advisedSleep + "ms"
            );
            Thread.sleep(advisedSleep);
        }
    }
    return obj;
}

, , , ObjectIds, 100% , , , .

, ObjectId ( , _id , ).

0

$lte ( ) , . ( " - 30 " ):

var threshold = new Date(); 
threshold.setMinutes(-30);

// now, query against a constant:
db.dates.find({"date" : {$lte : threshold}});

, , . #

var c = db.Collection.Find(Query.LTE("date", DateTime.UtcNow.AddMinutes(-30));
+4

Query Mongodb , , ... -

, . , . new Date() this.date.getTime() + 30 * 60000 .

0

, ! , , .

, Mongo, , $where javascript .

I suppose this really makes sense, given the fact that you can reference the "this" object - if javascript has not been reinstalled each time, then "this" actually means "first."

The effectiveness of this remains to be seen, especially since more and more tail-tails are being added to the collection.

0
source

All Articles