简体   繁体   English

如何使用对象数组过滤Google数据存储区实体

[英]How to filter google datastore entities with object array

Given a datastore entity structure where one column (dates) contains an object array: 给定一种数据存储实体结构,其中一列(日期)包含一个对象数组:

[{date: 2018-01-01T00:00:00Z, otherProps:x }, {date: 2018-01-02T00:00:00Z, otherProps: x}, {date: 2017-12-31T00:00:00Z, otherProps: x}]

Is it possible to construct a complex index and a query on the earliest date within the entities dates array. 是否可以在实体dates数组中的最早日期构造一个复杂的索引和查询。

For example, I would like to select all entities where the earliest date is before a certain date... This is my first foray into NoSQL so apologies if this is obvious! 例如,我想选择最早的日期在某个日期之前的所有实体...这是我第一次涉足NoSQL,因此很抱歉!

That may be tricky because query ordering and filtering are, in general, based on the property values and in your case the values would be the entire date arrays, not just the date values inside them. 这可能很棘手,因为查询排序和过滤通常基于属性值,并且在您的情况下,这些值将是整个日期数组,而不仅仅是其中的日期值。

Depending on the actual client library you use it might be possible. 根据实际使用的客户端库,这可能是可能的。 See, for example, Structured Properties and Filtering for Structured Property Values available with the ndb client library. 例如,请参见ndb客户端库提供的结构化属性结构化属性值的筛选

Personally I'd rather create separate entities for each of these date arrays, with the date and otherProps as properties - much simpler to use as sort/filter in queries. 我个人更愿意为每个日期数组创建单独的实体,并将dateotherProps作为属性-在查询中用作排序/过滤器要简单得多。 And in general a more scalable approach, IMHO, since it avoids the lists of (repeated) date array properties, see Creating your own activity logging in GAE/P . 一般而言,恕我直言,因为它避免了(重复的)日期数组属性列表,因此更具可扩展性,请参阅在GAE / P中创建自己的活动日志

You can reference to a element in a query by simple using arrayName.date < certainDate . 您可以简单地使用arrayName.date < certainDate引用查询中的元素。 It will work because the index is not composite, it means you are not trying to filter something like yourKind.property = value and arrayName.date < certainDate . 因为索引不是复合索引,所以它将起作用,这意味着您不会尝试过滤类似yourKind.property = value and arrayName.date < certainDate

I strongly recommend you not to save date as string but instead using a timestamp (like javascript (new Date()).getTime()), it will make it very much easier in the long therm, mainly if you are using more than one timezone. 我强烈建议您不要将日期另存为字符串,而是使用时间戳记(例如javascript(new Date())。getTime()),在长时间使用的情况下,这样做会非常容易,主要是如果您使用多个时区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM