sql >> Databáze >  >> NoSQL >> MongoDB

Mohu se dotazovat MongoDB ObjectId podle data?

Vkládání časových razítek do ObjectId velmi podrobně pokrývá dotazy založené na datech vložených do ObjectId.

Stručně v kódu JavaScript:

/* This function returns an ObjectId embedded with a given datetime */
/* Accepts both Date object and string input */

function objectIdWithTimestamp(timestamp) {
    /* Convert string date to Date object (otherwise assume timestamp is a date) */
    if (typeof(timestamp) == 'string') {
        timestamp = new Date(timestamp);
    }

    /* Convert date object to hex seconds since Unix epoch */
    var hexSeconds = Math.floor(timestamp/1000).toString(16);

    /* Create an ObjectId with that hex timestamp */
    var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");

    return constructedObjectId
}


/* Find all documents created after midnight on May 25th, 1980 */
db.mycollection.find({ _id: { $gt: objectIdWithTimestamp('1980/05/25') } });


  1. Spring data Match and Filter Nested Array

  2. Běží mongodb?

  3. Nejlepší způsob, jak se připojit k MongoDB pomocí Node.js

  4. MongoDB $ sqrt