- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
您如何将开放时间存储在文档(例如图书馆)中的 mongoDB 中,以便使用 Mongoid 轻松查询?我读过 this线程,但我不确定如何根据我的需要实现它。
我需要能够每天添加多个开放和关闭时间,因为图书馆应该能够在一天中的某些时间关闭然后重新打开。
我需要能够为这些营业时间添加异常(exception)情况。例如;在特定日期的星期一关闭。
请分享一些关于如何以最灵活的方式做到这一点的最佳实践和经验。
最佳答案
谢谢 Yeggeps 提供的需求列表。
这是根据您的要求修改的答案。当然,模式没有 chalice ,但我想在修改之前用一些示例数据+基于您的需求列表的查询来激发我的答案(保持平面结构易于查询和维护)。我重申,我并不是说这是最好的解决方案,但它是 一种查询简单且易于维护(恕我直言)的解决方案。
抱歉,代码有点快和脏。数据:
[
# library "lib1" open on wednesdays from 8:00 until 17:00
{"lib_id" => "lib1", "type" => "hours", "opening" => 800, "closing" => 1700, "day_of_week" => 3},
# library "lib1" open on wednesdays from 19:00 until 22:15
{"lib_id" => "lib1", "type" => "hours", "opening" => 1900, "closing" => 2215, "day_of_week" => 3},
{"lib_id" => "lib1", "type" => "hours", "opening" => 800, "closing" => 1700, "day_of_week" => 4},
{"lib_id" => "lib2", "type" => "hours", "opening" => 1100, "closing" => 1700, "day_of_week" => 3},
{"lib_id" => "lib2", "type" => "hours", "opening" => 1400, "closing" => 1700, "day_of_week" => 4},
{"lib_id" => "lib2", "type" => "hours", "opening" => 1900, "closing" => 2100, "day_of_week" => 4},
# library lib1 closed on wednesday december 7th 2011
{"lib_id" => "lib1", "type" => "closed_on", "reason" => "Rearranging the shelves", "closed_date" => Time.utc(2011, 12, 8)},
{"lib_id" => "lib2", "type" => "closed_on", "reason" => "We are closed for the holidays", "closed_date" => Time.utc(2011, 12, 7)}
].each do |schedule|
coll.save(schedule)
end
分别显示营业时间和特殊日期:
# List all the library id's distinctly
coll.distinct("lib_id").each do |lib_id|
puts "\nLibrary #{lib_id} opening hours:\n--- "
# I need to be able to show the opening hours in correlation with the Library
# Find all the opening hour information for current library
coll.find({"lib_id" => lib_id, "type" => "hours"}).each do |schedule|
puts " #{Date::DAYNAMES[schedule["day_of_week"]]}s: #{schedule["opening"]} - #{schedule["closing"]}" if schedule["type"] == "hours"
end
# I need to show an indication if it's open or closed in correlation with the Library.
puts "This library will be closed on: "
# Find all the exceptions for current lib_id -- introduce a time-period restriction using Date.utc (...)
coll.find({"lib_id" => lib_id, "type" => "closed_on"}).each do |closed|
puts " #{closed["closed_date"].strftime("%a %B%e, %Y")}: #{closed["reason"]}"
end
end
今天哪些图书馆开放?
# I need to be able to query on what's open right now or some time in the future with minute granularity
# here I'll also need to be able to exclude the Librarys that has added exceptions for the given time/day
puts "---"
qtime = (Time.now.hour * 100) + Time.now.min # minute granularity
qwday = Time.now.wday # this example only shows today
qclosed = Time.utc(Time.now.year, Time.now.mon, Time.now.mday)
# Query for all library ids which have opening times for this weekday, at this hour (+minutes)
coll.find({"opening" => {"$lte" => qtime}, "closing" => {"$gte" => qtime}, "day_of_week" => qwday}, {:fields => "lib_id"}).each do |lib|
# Check whether current library has an exception for this specific day
closed = coll.find_one({"lib_id" => lib["lib_id"], "closed_date" => qclosed})
if closed
# If an exception record was encountered, print the reason
puts "Library #{lib["lib_id"]} is normally open right now, but is now closed: '#{closed["reason"]}'"
else
# Else: the library is open
puts "Library #{lib["lib_id"]} is open right now! (#{Time.now.strftime("%a %B%e %Y, %H:%M")})"
end
end
产生如下输出:
Library lib1 opening hours:
---
Wednesdays: 800 - 1700
Wednesdays: 1900 - 2215
Thursdays: 800 - 1700
This library will be closed on:
Thu December 8, 2011: Rearranging the shelves
Library lib2 opening hours:
---
Wednesdays: 1100 - 1700
Thursdays: 1400 - 1700
Thursdays: 1900 - 2100
This library will be closed on:
Wed December 7, 2011: We are closed for the holidays
---
Library lib1 is open right now! (Wed December 7 2011, 13:12)
Library lib2 is normally open right now, but is now closed: 'We are closed for the holidays'
不可否认,我提出的解决方案的缺点是它没有捕获一个查询中的所有需求。
关于mongodb - 开放时间 MongoDB 的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375799/
有人查看我的代码说下面的 SQL 查询 (SELECT * FROM...) 显然容易受到攻击。我对此进行了研究,似乎我通过使用参数化查询正确地做到了这一点,但显然我遗漏了一些东西。 app.get(
我使用 Curie 作为基本模型,使用自定义数据集创建了一个微调模型。我正在使用 Azure OpenAI 服务。 该模型正在尝试使用最大可能的 token 生成响应。例如,如果 max_token
我正在尝试将6x15数组(映射)中的随机坐标设置为数字3,但前提是该坐标的值仍为0。我要放置的值为3) 25 int shipnum; 26 int x; 27 28 shipnum = 1;
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
您好,我正在寻找“开放”、“链接”和“多重哈希”算法的伪代码。是的,我花了很多时间在谷歌上搜索,但没能找到好东西。 如果你有分享的链接,我将不胜感激 问候 最佳答案 这hash table tutor
是否可以实现 Visitor Pattern尊重 Open/Closed Principle ,但仍然可以添加新的可访问类? 开放/封闭原则指出“软件实体(类、模块、函数等)应该对扩展开放,但对修改关
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 12 年前
我创建了 Azure 开放 AI 服务我想针对特定问题训练模型。但是,自定义模板按钮被禁用。在文档中,它说明了如何生成包含要训练的问题和答案的 .Jsonl,但没有任何内容可以导入这些文件。有人拿到了
我找到了一些关于开放/封闭递归的解释,但我不明白为什么定义中包含“递归”一词,或者它与动态/静态调度相比如何。我找到的解释中有: Open recursion. Another handy featu
我正在尝试为在加权图上运行的库找出类设计。可以在这个图上执行各种算法,例如,找到两个节点之间的最短距离,两个节点之间的最长距离,两个节点之间距离小于 10 的路径数(比方说)等。 我关心的不是我所知道
谁能告诉我开放 SSL 和安全 SSL 之间的区别。 我已经在 Play 商店发布了新的 Android 应用程序,它使用远程 API。 在 API 服务器上,我已经成功安装了安全 SSL。但我仍然被
当我将 url 加载到 WebView 并尝试打开链接时,其中一些会显示如下错误页面: net::ERR_UNKNOWN_URL_SCHEME intent://maps.yandex.ru?utm_
2 月 24 日消息 据外媒 TheVerge 报道,2 月 23 日晚间,LG 宣布将该公司的 webOS 智能电视平台授权给其他电视厂商使用,目前 LG 已和 RCA、Ayonz 和康佳等电视厂
我一直在思考这个面向对象的设计问题,但无法提出令人满意的解决方案,所以我想在这里向人群开放以征求意见。 我有一个代表回合制棋盘游戏的 Game 类,出于这个问题的目的,我们可以假设它类似于 Monop
我们有一个 Web 服务,它充当我们的客户和另一个服务之间的网关。客户端向第三方服务发送消息并从其接收随机消息。客户端的服务器通过安全套接字打开到我们的 Web 服务器的 channel ,以便接收传
应用商店里有一个叫 Touchpad 的应用,最后一次更新是在 11 月 29 日,其中包含一个新功能,支持“使用设备键盘上的 Siri 键向电脑发送文本”,我想知道是否有开放的 API Siri现在
我尝试在认知搜索上下文中为 Azure 开放 AI 服务添加矢量搜索时遇到问题。选择勾选标记后,即使有可用的现有文本嵌入模型,我也无法列出任何模型。造成这种差异的可能原因是什么以及如何解决? 最佳答案
我尝试在认知搜索上下文中为 Azure 开放 AI 服务添加矢量搜索时遇到问题。选择勾选标记后,即使有可用的现有文本嵌入模型,我也无法列出任何模型。造成这种差异的可能原因是什么以及如何解决? 最佳答案
我们的堆栈:Tomcat 7、Spring 3.1.1、OpenJPA 2.2.0 我遇到了一个问题,根源是比较: server1.equals(server2); server1 和 server2
我正在使用以下命令设置一个 java EE 项目(打开)JPA。我使用 glassfish 4.0 作为我的应用程序服务器,但似乎无法让持久性发挥作用。 我面临的问题似乎是一个相当普遍的问题,因为同一
我是一名优秀的程序员,十分优秀!