user2086641
user2086641

Reputation: 4371

Query in DocumentDB based on inner json value - C#

I have 2 objects in DocumentDB like below,

{
    "TrackGoods": {
        "Attributes": {
            "Attribute": {
                "BlockName": "Default",
                "Brand": "Default",
                "CaseType": "Carton",
            }
        },
        "Codes": [{
            "trackcode": "191155772333RIF1"
        },
        {
            "trackcode": "721141315199RIF2"
        }],
        "Time": "2017-12-18T13:35:04.0792480+05:30",
        "IpAddress": "127.0.0.1",
        "Lat": "155.00",
        "Long": "-202.00",
    }
}

{
    "TrackGoods": {
        "Attributes": {
            "Attribute": {
                "BlockName": "Default1",
                "Brand": "Default1",
                "CaseType": "Carton1",
            }
        },
        "Codes": [{
            "trackcode": "191155772333RIF3"
        },
        {
            "trackcode": "721141315199RIF4"
        }],
        "Time": "2017-12-18T13:35:04.0792480+05:30",
        "IpAddress": "127.0.0.1",
        "Lat": "155.00",
        "Long": "-202.00",
    }
}

I want to get the document where a Codes with trackcode='191155772333RIF1'.

I tried the following query and it is not returning the document from DocDB,

string selectdoc = "select doc.TrackGoods from doc join Codes in doc.Codes where Codes.trackcode = '191155772333RIF1'";
    query = new SqlQuerySpec(selectdoc,
        new SqlParameterCollection(new SqlParameter[] {
          new SqlParameter { Name = "@code", Value = code }
          })); 

Can any one help me to get the document from DocDB.

Thanks

Upvotes: 0

Views: 82

Answers (1)

Matias Quaranta
Matias Quaranta

Reputation: 15603

I tried with this query and it seemed to work correctly:

select doc.TrackGoods from doc join Codes in doc.TrackGoods.Codes where Codes.trackcode = '191155772333RIF1'

You were missing the .TrackGoods. in after the in.

For more information regarding JOIN, take a look at this article.

Upvotes: 1

Related Questions