使用单个ObjectID在数组内查询Mongodb

时间:2017-04-15 12:25:46

标签: mongodb mongodb-query

这是我正在尝试处理的json数据。我想在两个日期之间收到消息。数据已导入mongodb。

{
  "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\", \"icon.url@2x\", \"icon\", \"thumbnail.url@2x\", \"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
}

现在,我输入了这个mongo查询:

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

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

1 个答案:

答案 0 :(得分:0)

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