question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Efficient way to look up an item by Id?

See original GitHub issue

Is your feature request related to a problem? Please describe. I’d like to look up an item as efficiently as possible when only the Id of the item is available.

I had assumed that the ReadItemAsync method would support this, but it appears to require a PartitionKey. To be clear, a key is defined in the collection, I just don’t know it at certain times.

Describe the solution you’d like An efficient way of querying a document by Id when the PartitiondKey is unknown

Describe alternatives you’ve considered The best idea I have:

var feedIterator = _container.GetItemQueryIterator<TDataType>($"SELECT TOP 1 * FROM c WHERE c.id = '{documentId}'");
if (feedIterator.HasMoreResults)
{
	var document = await feedIterator.ReadNextAsync();
	return document.FirstOrDefault();
}

Additional context I just feel as though there must be a better way!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ealsurcommented, Jun 12, 2020

Your code is not draining the query and dealing with empty pages.

public async Task<TDataType> GetById(Guid documentId)
{
	var feedIterator = _container.GetItemQueryIterator<TDataType>($"SELECT TOP 1 * FROM c WHERE c.id = '{documentId}'");
        List<TDataType> results = new List<TDataType>();
	while (feedIterator.HasMoreResults)
	{
		var iterator = await feedIterator.ReadNextAsync();
                results.AddRange(iterator);
	}

	return results.FirstOrDefault();
}
0reactions
msftbot[bot]commented, Dec 15, 2021

Closing due to in-activity, pease feel free to re-open.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Faster way to select an element with a given id
The fastest way is: document.getElementById("foo");. Setting this to a variable for reuse will prevent the need to find it over and over ...
Read more >
Document: getElementById() method - Web APIs | MDN
The getElementById() method of the Document interface returns an Element object representing the element whose id property matches the ...
Read more >
Searching: getElement*, querySelector*
The method getElementById can be called only on document object. It looks for the given id in the whole document. querySelectorAll. By far,...
Read more >
Best Practices for finding Items by Item ID
Hey guys, how do you quickly find podio items from the Podio Item ID without always going through single items? (I do not...
Read more >
REST - how to get item ID and document name while ...
I am trying to query a document library via REST and get the item ID and the document name. The query needs to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found