2017-04-15 55 views
0

这是我正在尝试处理的json数据。我试图在两个日期之间得到消息。数据已经导入到mongodb中。现在Mongodb查询在单个ObjectID数组里面

{ 
    "items": [ 
    { 
     "date": "2017-04-06T09:46:20.387420+00:00", 
     "from": { 
     "id": 4624534, 
     "links": { 
      "self": "https://api.hipchat.com/v2/user/4624534" 
     }, 
     "mention_name": "holy", 
     "name": "holy god", 
     "version": "Y1ML0DRJ" 
     }, 
     "id": "38f90558-2a23-458b-b87b-88dbdf997f7a", 
     "mentions": [], 
     "message": "ping", 
     "type": "message" 
    }, 
    { 
     "date": "2017-04-08T04:30:44.240163+00:00", 
     "from": { 
     "id": 4624534, 
     "links": { 
      "self": "https://api.hipchat.com/v2/user/4624534" 
     }, 
     "mention_name": "holy", 
     "name": "holy god", 
     "version": "Y1ML0DRJ" 
     }, 
     "id": "822b81e0-8077-41d7-bc50-fc9e4eba7d9e", 
     "mentions": [], 
     "message": "https://twitter.com/", 
     "type": "message" 
    }, 
    { 
     "attach_to": "822b81e0-8077-41d7-bc50-fc9e4eba7d9e", 
     "card": "{\"style\": \"link\", \"description\": \"From breaking news and entertainment to sports and politics, get the full story with all the live commentary.\", \"format\": \"medium\", \"url\": \"https://twitter.com/i/hello\", \"title\": \"Twitter. It's what's happening.\", \"id\": \"https://twitter.com/i/hello\", \"validation\": {\"safehtmls\": [\"activity.html\"], \"safeurls\": [\"url\", \"images.image\", \"images.image-small\", \"images.image-big\", \"icon.url\", \"[email protected]\", \"icon\", \"[email protected]\", \"thumbnail.url\"]}, \"type\": \"link\", \"thumbnail\": {\"url\": \"https://pbs.twimg.com/ext_tw_video_thumb/850335753108324353/pu/img/T8cV-7bGbbguiRGV.jpg\", \"width\": 599, \"type\": \"image/jpeg\", \"height\": 337}, \"icon\": {\"url\": \"https://abs.twimg.com/a/1491551685/img/t1/favicon.svg\", \"type\": \"image\"}}", 
     "color": "gray", 
     "date": "2017-04-08T04:30:44.825185+00:00", 
     "from": "Link", 
     "id": "7ccaf2b9-09bb-45ac-a025-c93f1f7df745", 
     "mentions": [], 
     "message": "\n\n\n<p><b><a href=\"https://twitter.com/i/hello\">Twitter. It's what's happening.</a></b></p>\n\n\n<p>From breaking news and entertainment to sports and politics, get the full story with all the live commentary.</p>\n\n", 
     "message_format": "html", 
     "notification_sender": { 
     "client_id": "888aec94-afee-45d8-89f7-ae077fcc4a7c", 
     "id": "hipchat-clinky", 
     "type": "addon" 
     }, 
     "type": "notification" 
    }, 
    { 
     "date": "2017-04-08T09:39:00.468858+00:00", 
     "from": { 
     "id": 4624534, 
     "links": { 
      "self": "https://api.hipchat.com/v2/user/4624534" 
     }, 
     "mention_name": "abcholy", 
     "name": "holy god", 
     "version": "Y1ML0DRJ" 
     }, 
     "id": "8a0de0e0-c312-490e-afcc-b0b16404cd67", 
     "mentions": [], 
     "message": "second message", 
     "type": "message" 
    }, 
    { 
     "date": "2017-04-11T15:32:39.367744+00:00", 
     "from": { 
     "id": 4624534, 
     "links": { 
      "self": "https://api.hipchat.com/v2/user/4624534" 
     }, 
     "mention_name": "abcholy", 
     "name": "holy god", 
     "version": "Y1ML0DRJ" 
     }, 
     "id": "4c1a090c-cb71-4548-8f96-a03ec2f3fb3b", 
     "mentions": [], 
     "message": "https://gist.github.com/abcholy/6d86352e73eab21cdd4fe78b37bd5aa0", 
     "type": "message" 
    }, 
    { 
     "date": "2017-04-11T15:33:42.730696+00:00", 
     "from": { 
     "id": 4624534, 
     "links": { 
      "self": "https://api.hipchat.com/v2/user/4624534" 
     }, 
     "mention_name": "abcholy", 
     "name": "holy god", 
     "version": "Y1ML0DRJ" 
     }, 
     "id": "a42b5267-937b-4de5-8c51-de4625742a4a", 
     "mentions": [], 
     "message": "hello", 
     "type": "message" 
    } 
    ], 
    "links": { 
    "self": "https://api.hipchat.com/v2/room/3452990/history" 
    }, 
    "maxResults": 100, 
    "startIndex": 0 
} 

,我进入了这个蒙戈查询:

db.data.find({"_id" : ObjectId("58ee59f7f35120aaba26cff0")},{ items: { $elemMatch: { "date": {$gte:"2017-04-08T04:30:44.240163+00:00"} } } }) 

但只返回一个项目是第一个。如果我尝试$ lte,它也会返回单个项目,但我希望所有属于日期规范的项目。如何实现这一目标?

+3

可能重复[只检索MongoDB集合中的对象数组中的查询元素](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array -in-mongodb-collection) – Veeram

+0

没有重复的afaik。 –

回答

0

你的日期字段是一个字符串,它没有什么比字符串更大的概念。您需要将其转换为ISODate或其他日期格式才能使用$ gte或$ lte操作。