我需要在mongodb中设计一个计划模式,原始数据格式是这样的 {"schedule_begin":日期,"schedule_end":日期,"schedule_days_runs":"1111100"}
请注意,架构中存在一个带有数据"schedule_days_runs":"1111100"的属性schedule_days_runs。一周有 7 天,其中“1”代表工作,“0”代表不工作。
基本上,应用程序所做的就是查找在某个日期(例如星期一)工作的所有时间表,这意味着我们需要查询时间表,schedule_start =< one_day <= Schedule_end,同时确保查询的时间表也在周一运行。
一个简单的解决方案是直接将“1111100”以String的形式存储到mongodb中,只使用schedule_start和schedule_end进行查询,然后使用应用逻辑来获取结果。然而,这个解决方案可能不太方便,因为通常我会收到数千个查询,并且一半的查询不是答案。例如,要获取特定日期“1111100”的工作时间表(该时间表工作于周一至周五),查询结果给了我很多无用的东西,例如“0000011”(该时间表工作于周末)。
我正在尝试找出更好的解决方案,有人可以有更好的主意吗?
最佳答案
我仍在使用 mongodb
迈出第一步,但我认为使用具有不同值的数组会更适合 mongodb
。大致如下:
{
"schedule_begin": Date,
"schedule_end":Date,
"schedule_days_runs": ["mo", "tu", "we", "th", "fr", "sa", "su"]
}
然后您可以使用如下查询:
{ schedule_days_runs: "mo" }
让所有这些都在周一运行。
据我所知,其中可能存在一些拼写错误。另外,据我所知,对于 mongodb,我们必须摆脱我们在关系数据库中学到的几乎所有内容,这是另一回事。使用全新的方法让我解决了一些使用关系方法难以解决的问题。
我用这个问题作为引用:Querying array elements with Mongo
要找出数组的长度,这道题有用:In Mongo DB, how do I find documents where array size is greater than 1
引用:
using $where
db.accommodations.find( { $where: "this.name.length > 1" } );
因此基于此,您应该尝试:
{ $where : "this.schedule_days_runs.length > 1" }
关于mongodb - 需要一个更好的模式来查询 mongodb 中的时间表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220185/