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.

  1. *[_type == 'message']: select all documents of type 'message'.
  2. |: pipe the messages (so we can perform the rest of the operations)
  3. order(_createdAt desc): order the messages from newest to oldest (_createdAt is set automatically by Sanity when a document is created)
  4. [0]: select the first message from the list (which is also the newest)
  5. ._id: select the _id of the newest message

To fetch another property, multiple properties or the entire message object, replace the last part of the query.



来源:https://stackoverflow.com/questions/50154221/fetch-id-of-last-created-document-of-given-type-in-sanity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!