公告
财富商城
积分规则
提问
发文
2019-07-30 08:57发布
Animai°情兽
In Sanity, for a given document type named message, how can I get the _id of the newest message document?
message
_id
You can actually do that in a single query in GROQ (Sanity's query language):
*[_type == 'message'] | order(_createdAt desc) [0] ._id
This query has five parts.
*[_type == 'message']
'message'
|
order(_createdAt desc)
_createdAt
[0]
._id
To fetch another property, multiple properties or the entire message object, replace the last part of the query.
最多设置5个标签!
Query
You can actually do that in a single query in GROQ (Sanity's query language):
Query Explanation
This query has five parts.
*[_type == 'message']
: select all documents of type'message'
.|
: pipe the messages (so we can perform the rest of the operations)order(_createdAt desc)
: order the messages from newest to oldest (_createdAt
is set automatically by Sanity when a document is created)[0]
: select the first message from the list (which is also the newest)._id
: select the_id
of the newest messageTo fetch another property, multiple properties or the entire message object, replace the last part of the query.