-1

When I run a select query ("Select top 1 * from c") on a container I get:

{
    "id": "102",
    "sid": "s-102",
    "name": "Bena",
    "grade": "2",
    "_rid": "ruxSAIiGPgYCAAAAAAAAAA==",
    "_self": "dbs/ruxSAA==/colls/ruxSAIiGPgY=/docs/ruxSAIiGPgYCAAAAAAAAAA==/",
    "_etag": "\"00006000-0000-1900-0000-5f4354320000\"",
    "_attachments": "attachments/",
    "_ts": 1598247986
}

I don't want _rid, _self,_etag etc, I just want the entities eg:

{
    "id": "102",
    "sid": "s-102",
    "name": "Bena",
    "grade": "2"
}

Current code:

    CosmosDatabase db1=client.getDatabase("Student");
    CosmosContainer c=db1.getContainer("Stu"); 
    CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
    options.setMaxBufferedItemCount(5);
    String sql = "SELECT TOP 1 *  FROM  s ";
    CosmosPagedIterable<JSONObject> cpi=c.queryItems(sql, options, JSONObject.class);
rohini
  • 1
  • 5
  • Does this answer your question? [Avoid getting back DocumentDb system properties](https://stackoverflow.com/questions/32281389/avoid-getting-back-documentdb-system-properties) – Martin Brandl Aug 25 '20 at 07:08
  • Can you show us your code?If you use java sdk v4,you can try this code:`CosmosPagedIterable pagedIterable = container.queryItems("select top 1 * from c",new QueryRequestOptions(), your entity.class);` – Steve Johnson Aug 25 '20 at 08:09
  • @SteveZhao yes, I am using something similar to this, but I get _rid, _self, etc values. I am using a JSONObject and then parsing the values. So when I parse I get all the values, were as if you use a specific Object (model class object) will map to the object which in my case can't be done. – rohini Aug 25 '20 at 09:27

2 Answers2

0

You should create a class of your entity(do not use JSONObject). This is my simple test code:

database = client.getDatabase(databaseName);
container = database.getContainer(containerName);
CosmosPagedIterable<User> usersPagedIterable  = container.queryItems("select top 1 * from c",new QueryRequestOptions(), User.class);
if(usersPagedIterable.iterator().hasNext()){
    User user = usersPagedIterable.iterator().next();
    System.out.println(user.toString());
}

The screenshot of debug(There is no _rid, _self,_etag etc): enter image description here

Steve Johnson
  • 8,057
  • 1
  • 6
  • 17
  • Creating a model class can be done if I am using a single container to store my data. In my case, I have to read data from multiple container having different structure for each items. So I am avoiding this. I wanted to know if there is direct query (or a inbuild method in cosmosdb java sdk) which returns only the entity I added and not the database properties entity. And you are not getting the properties because you have a model class for that container, which in my case not supported.Thanks :) – rohini Aug 26 '20 at 09:59
  • I see that.As far as I know,there is no such method.Maybe you need to delete properties on your client side.Or create udf and delete those properties. – Steve Johnson Aug 27 '20 at 02:04
  • Is there a way to find the datatype of the element dynamically? Like I don't want to check each entity against each datatype as mentioned here https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-type-checking-functions – rohini Aug 27 '20 at 04:55
0

Change the SQL to the following SELECT TOP 1 s.id, s.sid, s.name, s.grade, FROM s

If you want the result to be deserialized into your domain object, just create one as a java bean and pass it to queryItems method instead of "JSONObject.class"

RCT
  • 1,044
  • 1
  • 5
  • 12