MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

MongoDB/Mongoose - Menanyakan array objek berdasarkan tanggal

Anda dapat menjalankan pipeline agregasi yang menggunakan $filter operator pada values Himpunan. Kueri mongo shell berikut menunjukkan hal ini:

var start = new Date("2016-10-28T07:00:00.000Z"),
    end = new Date("2016-11-03T07:00:00.000Z");

db.metrics.aggregate([
    { 
        "$match": { 
            "name": "Hello",
            "values.date": { "$gt": start, "$lt": end }
        } 
    },
    {
        "$project": {
            "name": 1,
            "values": {
                "$filter": {
                    "input": "$values",
                    "as": "value",
                    "cond": { 
                        "$and": [
                            { "$gt": [ "$$value.date", start ] },
                            { "$lt": [ "$$value.date", end ] }
                        ]
                    }
                }
            }
        }
    }
])

Contoh Keluaran

/* 1 */
{
    "_id" : ObjectId("5845453145fda1298fa50db9"),
    "name" : "Hello",
    "values" : [ 
        {
            "value" : 1568,
            "date" : ISODate("2016-10-29T07:00:00.000Z"),
            "_id" : ObjectId("58453abfef7aaa15ac1fdee7")
        }, 
        {
            "value" : 1547,
            "date" : ISODate("2016-10-30T07:00:00.000Z"),
            "_id" : ObjectId("58453abfef7aaa15ac1fdee6")
        }, 
        {
            "value" : 1497,
            "date" : ISODate("2016-10-31T07:00:00.000Z"),
            "_id" : ObjectId("58453abfef7aaa15ac1fdee5")
        }, 
        {
            "value" : 3031,
            "date" : ISODate("2016-11-01T07:00:00.000Z"),
            "_id" : ObjectId("58453abfef7aaa15ac1fdee4")
        }, 
        {
            "value" : 2559,
            "date" : ISODate("2016-11-02T07:00:00.000Z"),
            "_id" : ObjectId("58453abfef7aaa15ac1fdee3")
        }
    ]
}

Untuk MongoDB 3.0, solusi berikut berlaku:

var start = new Date("2016-10-28T07:00:00.000Z"),
    end = new Date("2016-11-03T07:00:00.000Z");

db.metrics.aggregate([
    { 
        "$match": { 
            "name": "Hello",
            "values.date": { "$gt": start, "$lt": end }
        } 
    },
    {
        "$project": {
            "name": 1,
            "values": {
                "$setDifference": [
                    {
                        "$map": {
                            "input": "$values",
                            "as": "value",
                            "in": {
                                "$cond": [
                                    { 
                                        "$and": [
                                            { "$gt": [ "$$value.date", start ] },
                                            { "$lt": [ "$$value.date", end ] }
                                        ]
                                    },
                                    "$$value",
                                    false
                                ]
                            }
                        }
                    },
                    [false]
                ]
            }
        }
    }
])


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Menyebarkan Database Cloud dengan ClusterControl 1.6

  2. NoClassDefFoundError setelah mengekspor toples

  3. Ketika kursor mongodb akan kedaluwarsa

  4. Hubungan dalam database berorientasi dokumen?

  5. Apakah koneksi ke MongoDB secara otomatis ditutup pada process.exit()?