OrientDB 中嵌入列表的范围日期时间查询

Range date time query on embedded list in OrientDB

我在 OrientDB 中有一个这样的结构,其中每个节点都是一个特定的用户,由 user_id 表示,他们所有的购买信息都保存在嵌入列表中,如下所示。

[
 {
  "user_id": 1,
  "purchase_info": [ {
                      "product_id": 100,
                      "timestamp": "2016-04-26  01:50:00"
                   }, 

                   {
                      "product_id": 200,
                      "timestamp": "2016-04-26  10:50:00"
                   }

                   ]
 },

 {
  "user_id": 2,
  "purchase_info": [ {
                      "product_id": 100,
                      "timestamp": "2016-04-26  10:50:00"
                   }, 

                   {
                      "product_id": 300,
                      "timestamp": "2016-04-26  20:50:00"
                   }
                   ]
 },
 {
  "user_id": 3,
  "purchase_info": [ {
                      "product_id": 100,
                      "timestamp": "2016-04-26  04:50:00"
                   }
                   ]
 },
 {
  "user_id": 4,
  "purchase_info": [ {
                      "product_id": 300,
                      "timestamp": "2016-04-26  05:50:00"
                   }
                   ]
 }
]

现在我想查找在特定日期时间范围内购买产品的用户。

例如,如果日期时间范围介于 2016-04-26 01:00:00 和 2016-04-26 04:55:00 之间,returning 结果将是 user_id 1 和 3.

我知道要查询嵌入式列表,我们需要使用 WHERE <value> IN <key name>。但是,我找不到使用该格式使用范围的方法。

此外,对于时间戳的非范围查询,无法使用 asDateTime()

例如这个查询没有return任何东西:

SELECT from V where "2016-04-26 01:50:00" in purchase_info.timestamp.asDateTime()

这不是使用 OrientDB 的最佳方式。

  1. 不要为连接设置产品 ID,而是在产品和用户实体之间创建一个边缘。边缘是"purchase"。 OrientDB 是一个图形数据库。这样就可以跨越两边的关系了。
  2. 不要将日期存储为字符串,而是存储为日期时间,因此查询中不需要转换。此外,如果您使用具有配置的日期时间格式的字符串,OrientDB 会在查询评估期间自动将您的字符串转换为日期时间

如果您按照上面的建议更改模型,您可以执行如下查询:

select expand( inV() )
from purchase
where date between "2016-04-26 01:00:00" and "2016-04-26 04:55:00"

获取在该时间间隔购买的所有用户,并且:

select expand( outV() )
from purchase
where date between "2016-04-26 01:00:00" and "2016-04-26 04:55:00"

在该时间间隔内售出所有产品。

为了获得良好的性能,请记住使用 NOTUNIQUE 索引对边缘 "Purchase" 中的日期字段进行索引。

试试这个:

select user_id,date from (select user_id,purchase_info.timestamp as date from User unwind date) where date between "2016-04-26 01:00:00" and "2016-04-26 04:55:00"

结果:

希望对您有所帮助。