Search code examples
sanity

Fetching documents


Using Sanity Studio I've created 2 documents in a simple schema "users" (the structure is not that important), and then updated each of them with some values, and published.

Trying to get the results using the query:

*[_type=="users"]

instead of getting 2 documents, I've got 4, and it seems each update created a new revision and all revisions of all documents were returned.

Is this how Sanity's edit works each time? If so, how do I just get the 2 documents with all the latest changes?


Solution

  • It sounds like you're probably getting both the published and the draft versions of each document. A draft will have the same _id as the published document but will be prepended with drafts..

    This might be a bit clearer if you modify your query to the following, which will return an array of _ids:

    *[_type=="users"]._id
    

    If this is in fact what's happening, you can modify your query to exclude drafts:

    *[_type=="users" && !(_id in path("drafts.**"))]
    

    Here's a bit more detail about drafts.