简体   繁体   中英

Azure Table Storage: Translate C# to NodeJS - How to use CompareTo?

There are a lot of examples circulating around the internet about how you can use CompareTo with Azure Table Storage to basically do some substring searching from C# , but I can't find anything showing how to do it in NodeJS and all the syntaxes I've tried to put together just throw back various syntax or invalid query storage exceptions.

Can anyone tell me how to do the equivalent of the C# example on this website, but from NodeJS? https://lifeportal.azurewebsites.net/azure-table-storage-searching-entities-using-query-like-substring-or-left/

string projectIdString = projectId.ToString().ToLower(); // projectId - Guid
string startQuery = projectIdString + "_"; // Our separate symbol
string endQuery = projectIdString + "`"; // Next symbol in ASCII table after "_"

Expression<Func<DynamicTableEntity, bool>> filters = (e.RowKey.CompareTo(startQuery) >= 0 && e.RowKey.CompareTo(endQuery) < 0);
CloudTable table = _storageContext.Table(Tables.Users);

bool result = await DeleteAllEntitiesInBatches(table, filters);
return result;

What seems like the most obvious of:

const TABLE_NAME = 'MYTABLE';

var azure = require('azure-storage');
var tableService = azure.createTableService();
var query = new azure.TableQuery()
    .where('PartitionKey eq ?', 'MYKEY').and('RowKey.compareTo(\'1-\') ge ?', 0);

tableService.queryEntities(TABLE_NAME, query, null, function(error, result, response) {
    if (!error) {
        // result.entries contains entities matching the query
    }
});

Results in:

"An unknown function with name 'RowKey.compareTo' was found. This may also be a key lookup on a navigation property, which is not allowed."

It looks like you are attempting to query RowKey with a StartsWith . In node.js you can use operator ge (greater than or equal) to do that.

So your code would be something like:

var query = new azure.TableQuery()
    .where('PartitionKey eq ?', 'MYKEY').and('RowKey ge ?', '<starts with substring>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM