groq

Fetch _id of last created document of given type in Sanity

徘徊边缘 提交于 2019-12-24 07:13:35
问题 In Sanity, for a given document type named message , how can I get the _id of the newest message document? 回答1: Query You can actually do that in a single query in GROQ (Sanity's query language): *[_type == 'message'] | order(_createdAt desc) [0] ._id 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