使用n1ql筛选文档 [英] Filter documents using n1ql
本文介绍了使用n1ql筛选文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两种类型的文档,它们之间存在一对多的关系,我只需要根据最新的日期值获得一份文档。这是一个示例:
I have two types of documents with a relation one to many and I need to get only one document basing on the latest date value. Here it is an example:
文档类型(歌手):
{
"id":"223-34fd-35325-dsf-343f",
"resourceType":"Singer",
"name":"Paul"
}
文档类型(相册1):
{
"id":"456-d435-43g346-43-436tf-345g",
"name":"Amazing night",
"author":"223-34fd-35325-dsf-343f",
"creationDate": "2017-05-12T07:57:51.458Z"
}
文档类型(相册2):
{
"id":"878-ffe6-43gs56-5hn7-ffgdd-345hd",
"name":"Be quiet",
"author":"223-34fd-35325-dsf-343f",
"creationDate": "2017-05-11T13:43:05.580Z"
}
我需要获得只有一张专辑的歌手列表(这是最后一张添加的专辑,基于 creationDate
)。就我而言,我必须得到歌手 Paul和专辑 Amazing night。
Well, I need to get a list of Singers with only one album (which was the last added, basing on creationDate
). In my case I have to get singer "Paul" and the album "Amazing night". Is this either possible?
推荐答案
如果需要相册文档,请使用以下查询。
If You need Album document use the following query.
SELECT singer, MAX([a.creationDate, a])[1] latestAlbum
FROM default a
WHERE a.author IS NOT MISSING
GROUP by a.author
LETTING singer = (SELECT RAW s.name FROM default s USE KEYS a.author)[0];
这篇关于使用n1ql筛选文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文