Javascript 用时间索引两个字段
我对数据库进行了Javascript 用时间索引两个字段,javascript,indexing,rethinkdb,rethinkdb-javascript,Javascript,Indexing,Rethinkdb,Rethinkdb Javascript,我对数据库进行了referencedb和数据/表格,比如“新闻”和大量数据行: [ { "author": "author1", "category_id": "business", "country": "id", "created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00", "description": "description", "id": "74c25662-7f94-47ef-8a7e-50
referencedb
和数据/表格,比如“新闻”和大量数据行:
[
{
"author": "author1",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author2",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description2",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "sport",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description3",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description4",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
}
.....
]
我需要为category\u id
和created\u at
(timestamp
)创建索引,并查询特定类别,仅按天过滤(特定日期)。我想优化和加快查询结果
我可以在javascript
中通过如下过滤器对category\u id
business
和day
15进行过滤:
r.table("news").filter({category_id: 'business'}).filter(
r.row("created_at").day().eq(15)
)
但是我如何为category\u id
和created\u-at
创建索引,并在created\u-at
的某一天查询它
r.table("news").indexCreate( ???
谢谢应该是这样的:
r.table('news').indexCreate('businessAndDate', function (doc) {
return doc('category_id').add(doc('created_at').day().coerceTo('string'));
});
然后你可以:
r.table('news').getAll('business15', {index: 'businessAndDate'})
你可能想在数据之间插入一个分隔符(比如'business-15'
),并且使用的不仅仅是一天(否则,为什么要有一个完整的时间戳呢?),这取决于你和你愿意写多少个。添加。)
根据,这称为任意索引