问题
Does anyone know how to use a SQL query on a CosmosDB Collection that has a hyphen in the name? For example
SELECT * FROM product-group where product-group.id = "3829503"
I've see that properties can be accessed using the [""] syntax but haven't seen any solutions for the actual collection name.
回答1:
When you query via DocumentDB SQL, you don't need to specify the exact collection name; you're really specifying an alias in your query.
In the Query Explorer (via the portal), all queries are done by first selecting a collection. At that point, use a simple alias to query. For example, here is a query against a collection called sample-data
(as selected in the "Collections" dropdown), but I don't need to specify the actual collection name in the query. I simply use mycollection
as an alias for sample-data
:
And in code, the SDK call includes both a database parameter and a collection parameter, again obviating the need for the actual collection name in the query. For example, the same query, called from c#, might look something like this:
client.CreateDocumentQuery<MyObject>(
UriFactory.CreateDocumentCollectionUri("mydb", "sample-data"),
"SELECT * FROM mycollection WHERE mycollection.id = '3829503'");
来源:https://stackoverflow.com/questions/46719930/how-to-use-sql-query-with-hyphen-in-collection-name