dongliling6336 2016-06-06 06:14
浏览 171

我需要找出特定时间之间匹配的结果,除了mongo中的日期

{
"_id" : ObjectId("5754f6b904896f7e0c8b45e5"),
"coordinates" : [
    76.7033471,
    30.7104586
],
"delivery_delay" : NumberLong(0),
"delivery_radius_in_km" : 10,
"delivery_slot_duration" : NumberLong(30),
"description" : "wgwhsh",
"image" : "item_5751680404896f7e0c8b45dd_15755.png",
"image_thumbnail" : "item_5751680404896f7e0c8b45dd_15755.png",
"item_id" : "5751680404896f7e0c8b45dd",
"pick_up_or_delivery" : "Delivery",
"price_in_primary_currency" : 300,
"primary_currency" : "INR",
"quantity_available" : NumberLong(6),
"quantity_delivered" : NumberLong(10),
"quantity_ordered" : NumberLong(100),
"recurring" : true,
"repeat_every_friday" : true,
"repeat_every_monday" : true,
"repeat_every_saturday" : true,
"repeat_every_sunday" : true,
"repeat_every_thursday" : true,
"repeat_every_tuesday" : true,
"repeat_every_wednesday" : true,
"restriction" : [
    "Vegetarian",
    "Beef free"
],
"sale_event_latitude" : "30.7104586",
"sale_event_longitude" : "76.7033471",
"seller_address_id" : "5751142604896f7e0c8b45c8",
"seller_designation_as_shown_to_buyers" : "good selle",
"seller_rating" : 4,
"seller_user_id" : "575110eb04896f7e0c8b45bf",
"title" : "anamak",
"type" : [
    "Indian"
],
"pickup_date_delivery_and_time_from" : ISODate("2016-06-06T04:05:54Z"),
"pickup_delivery_date_and_time_to" : ISODate("2016-06-14T04:06:54Z"),
"cut_off_time" : ISODate("2016-06-10T04:06:54Z"),
"status" : true,
"updated_at" : ISODate("2016-06-06T04:06:17.017Z"),
"created_at" : ISODate("2016-06-06T04:06:17.017Z")

}

I have a collection with above record ,there are two date object:

1->pickup_date_delivery_and_time_from

2->pickup_delivery_date_and_time_to

Above fields are stored with timestamps .Now i want to make a query for get all result where "pickup_date_delivery_and_time_from(only time)" is less than 04:05:14Z and "pickup_delivery_date_and_time_to(only time)" should be greater than 04:05:14Z leave the date from above fields.

Only time should be taken in concern.

I have to match above fields with current time.

Any help!!!

  • 写回答

2条回答 默认 最新

  • dppfxf909679 2016-06-06 07:36
    关注

    The best way is to maintain a separate time field along with the date to get the best performance.

    If you cannot change the schema and if you are bound to use what you have, there are two approaches.

    1. Use $where

    db.mycollection.find({$where : function() { return <your matching criteria>} })

    Last resort. This would give a performance hit.

    1. Use aggregation

      { "_id" : 1, ISODate("2014-01-01T08:15:39.736Z") } db.mycollection.aggregate( [ { $project: { year: { $year: "$date" }, month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, hour: { $hour: "$date" }, minutes: { $minute: "$date" }, seconds: { $second: "$date" }, milliseconds: { $millisecond: "$date" }, dayOfYear: { $dayOfYear: "$date" }, dayOfWeek: { $dayOfWeek: "$date" }, week: { $week: "$date" } } } ] )

    and use other pipeline operators to match according to your need.

    check out $where and aggregation with date

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘