2015-10-20 181 views
0

我已经在此搜索了很多主题,但似乎没有一个可行。仅限MongoDB返回在最近一小时/ 24小时内更新的字段

我得到的最接近的形式在这里:

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是否可以伸出援助之手?

谢谢!

回答

0

而不是使用的_idtimestamp(这将失败,因为它不是一个有效的ObjectId),通过创建保存日期对象24小时前的变量做的"created_on"日期字段的日期范围查询并使用​​运营商在查询:

var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000)); 
db.collection.find({ "created_on": { "$gte": yesterday } }); 
+0

您好!在test.js中试过你的脚本,然后做了一个mongo localhost:27017/** test.js.它只有一条消息:连接到:。 – Tony

+0

您可以尝试在mongo shell中针对实际集合名称运行查询吗? – chridam