MongoDB-Only返回在过去一小时/ 24小时内更新的字段

时间:2015-10-20 18:00:17

标签: linux mongodb database

我已经搜索了很多线程,但似乎都没有。

我得到的最接近的是:

db.<collection>.find({ $where: function () { return Date.now() - this._id.generation_time() < (24 * 60 * 60 * 1000)  }  })

但它给了我这个错误:

error: {
    "$err" : "TypeError: Object edge:Package-****-34d7-4313-a4b9-13b5863fadf2 has no method 'generation_time' near 'this._id.generation_time() < (24 * 60 * ' ",
    "code" : 16722

这种特殊的数据格式似乎有些不对劲。

数据库的一个部分:

  {
        "_id" : "edge:Package-1e63f289-022e-4678-b806-72244277db54",
        "data_1442611395145" : {
                "ulid" : ObjectId("55fc4883e1382301adfde5dd"),
                "etlp" : "AMBER",
                "api" : {
                        "indicators" : [
                                {
                                        "idref" : "CISCP:indicator-161e129e-3647-48fb-ab4c-ff1bea0e08a9"
                                }
                        ],
                        "timestamp" : "2015-09-18T17:23:15.145333+00:00",
                        "version" : "1.1.1",
                        "id" : "edge:Package-1e63f289-022e-4678-b806-72244277db54",
                        "stix_header" : {
                                "handling" : [
                                        {
                                                "controlled_structure" : "../../../../descendant-or-self::node()",
                                                "marking_structures" : [
                                                        {
                                                           "color" : "AMBER",
                                                           "xsi:type" : "tlpMarking:TLPMarkingStructureType"
                                                        },
                                                        {
                                                           "xsi:type" : "simpleMarking:SimpleMarkingStructureType",
                                                           "statement" : "FOUO"
                                                        }
                                                ]
                                        }
                                ]
                        }
                },
                "hash" : ".cdb111212d2706be3dcb58b896cd1629993fa951",
                "idns" : "http://soltra.com/",
                "etou" : [ ],
                "esms" : [
                        "FOUO"
                ],
                "edges" : {
                        "CISCP:indicator-161e129e-3647-48fb-ab4c-ff1bea0e08a9" : "ind"
                },
                "summary" : {
                        "intent" : [ ]
                }
        },
        "created_on" : ISODate("2015-09-18T17:23:15.490Z"),
        "created_by_username" : "admin",
        "data" : {
                "ulid" : ObjectId("55fc4883e1382301adfde5dd"),
                "etlp" : "AMBER",
                "api" : {
                        "indicators" : [
                                {
                                        "idref" : "CISCP:indicator-161e129e-3647-48fb-ab4c-ff1bea0e08a9"
                                }
                        ],
                        "timestamp" : "2015-09-18T17:23:15.145333+00:00",
                        "version" : "1.1.1",
                        "id" : "edge:Package-1e63f289-022e-4678-b806-72244277db54",
                        "stix_header" : {
                                "handling" : [
                                        {
                                                "controlled_structure" : "../../../../descendant-or-self::node()",
                                                "marking_structures" : [
                                                        {
                                                           "color" : "AMBER",
                                                           "xsi:type" : "tlpMarking:TLPMarkingStructureType"
                                                        },
                                                        {
                                                           "xsi:type" : "simpleMarking:SimpleMarkingStructureType",
                                                           "statement" : "FOUO"
                                                        }
                                                ]
                                        }
                                ]
                        }
                },
                "hash" : "c45722264e9d129154c2b01ad9e0709e836e2fe8",
                "idns" : "http://soltra.com/",
                "etou" : [ ],
                "esms" : [
                        "FOUO"
                ],
                "edges" : {
                        "CISCP:indicator-161e129e-3647-48fb-ab4c-ff1bea0e08a9" : "ind"
                },
                "summary" : {
                        "intent" : [ ]
                }
        },
        "cv" : "1442611395145",
        "versions" : [
                "1442611395145"
        ],
        "created_by" : ObjectId("559450775bf8065a2d6476c1"),
        "tg" : [ ],
        "type" : "pkg",
        "created_by_organization" : null
}
{
        "_id" : "CISCP:indicator-161e129e-3647-48fb-ab4c-ff1bea0e08a9",
        "created_on" : ISODate("2015-09-18T17:23:15.491Z"),}

我想知道是否有办法让我使用“时间戳”与Date.now()进行比较.....尝试了一些已经无效的事情......想知道是否有任何GURU可以借出一个手?

谢谢!

1 个答案:

答案 0 :(得分:0)

而不是使用_id timestamp (由于它不是有效的 ObjectId而失败),通过创建24小时前保存日期对象的变量并在您的{strong> $gte 运算符查询:

"created_on"