Fetching documents

Viewed 460

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?

2 Answers

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.

@Geoff's answer is correct, however I've found a better way to do this if someone does not care about keeping track of drafts in the first place (my case). You can specify a

liveEdit: true

flag on the schema type, in which case drafts will not be tracked for this type at all

Related