can you model a document as one of the property in a vertex in Gremlin DB ?

nithish reddy minupuri 126 Reputation points
2021-09-10T17:14:35.84+00:00

property("States", {"DeviceCount":"12","SC_ItemStatus":"2","SC_ItemWarningStatus":"0","SC_SStandardPerc":"1"}).

How do you query it if you wanted to find DeviceCount? I know that i can model it using another vertex to state and relationship between first vertex and states. Checking if there is a alternative to it and wanted to know how that affects different reads?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,456 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anurag Sharma 17,576 Reputation points
    2021-09-14T06:45:40.39+00:00

    Hi @nithish reddy minupuri , thanks for replying back.

    <Writing it as another answer as word limit is crossing 1600 characters>

    Id is unique identifier which is assigned to every item in Azure Cosmos DB while these items are created. Index concept is similar to what we have on other databases .Indexes are created by default in Cosmos DB on every property and they help us in writing the efficient and less expensive queries. This means if we exclude any property from index then the same query could take more RUs or more time to execute compared to the indexed properties which would then add to the cost. Now we might not notice any change for few documents but when the documents go to millions or more this would significantly impact the cost.

    For retrieving all the properties lets take below examples:
    Here we are adding 2 vertex:

    g.addV('person').property('id', 'mary').property('firstName', 'Mary').property('lastName', 'Andersen').property('age', 39).property('pk', 'pk')  
    g.addV('person').property('id', 'steve').property('firstName', 'Steve').property('lastName', 'Wood').property('age', 40).property('pk', 'pk')  
    

    So if we run below query we will get both these with all the properties:

    g.V().hasLabel('person').has('age', gt(38))  
    

    Result:

    131851-image.png

    Same way we can write more queries depending upon the structure of our document.

    Please let me know if this helps or we can discuss further on the same.

    ----------

    Please don't forgot to click on accept it as answer button 131823-image.png wherever the information provided helps you. This can be beneficial to other community members as well..

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anurag Sharma 17,576 Reputation points
    2021-09-13T07:46:28.463+00:00

    Hi @nithish reddy minupuri , welcome to Microsoft Q&A forum.

    We can add a vertex with JSON object as below. I just used couple of attribute in JSON object, you can add as per requirements.

    g.addV('person').property('States','{\"DeviceCount\":\"15\",\"SC_ItemStatus\": \"6\"}').property('pk', 'pk')  
    

    Then we can query it something like this. Here we are doing a contain search on value.

    g.V().hasLabel('person').has('States', TextP.containing('DeviceCount'))  
      
    OR  
      
    g.V().hasLabel('person').has('States', TextP.containing('"DeviceCount":"15",'))  
    

    Now as we are using a contain query it is more expensive than a direct query.

    Direct Query:
    131465-image.png

    Contain Query:
    131531-image.png

    We notice the contain query is more expensive than the other query. This search is done on couple of vertices but if we have more it will get more expensive. Just to add, Azure Cosmos adds default indexes on every property.

    As per me I would recommend if we create another vertex to state and then have a relationship as you mentioned. It would be easier to manage as well.

    Please let me know if this helps or else we can discuss further on the same.

    ----------

    Please don't forgot to click on accept it as answer button 130202-image.png wherever the information provided helps you. This can be beneficial to other community members as well.