For example I have a document below for collection = delivery:
{
"doc": [
{
"docid": "15",
"deliverynum": "123",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
},
{
"docid": "16",
"deliverynum": "456",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
},
{
"docid": "17",
"deliverynum": "999",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
}
],
"id": "123",
"cancelled": false
}
is it possible to do a search with "deliverynum" = 999 and the output would be like below?
{
"doc": [
{
"docid": "17",
"deliverynum": "999",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
}
],
"id": "123",
"cancelled": false
}
or should I make another Collection just for the Doc part?
I am having trouble making a query in C# for this kind of scenario.
In Mongo shell you can use the $(projection) operator:
db.collection.find({ "doc.deliverynum": "999" }, { "doc.$": 1 })
Corresponding C# code can look like below:
var q = Builders<Model>.Filter.ElemMatch(x => x.doc, d => d.deliverynum == "999");
var p = Builders<Model>.Projection.ElemMatch(x => x.doc, d => d.deliverynum == "999");
var data = Col.Find(q).Project(p).ToList();
You can also use q = Builders<Model>.Filter.Empty if you want to get all documents even if the don't contain deliverynum =``999
Related
I have such items in Dynamo DB:
[{
"Id": "1",
"Data": {
"Value": "test"
}
},
{
"Id": "2",
"Data": {}
},
{
"Id": "3"
},
{
"Id": "4",
"Data": {
"Wrong": "234"
}
}]
And I'm trying to make it flatten, but for the Data.Value field only:
[{
"Id": "1",
"Value": "test"
},
{
"Id": "2"
},
{
"Id": "3"
},
{
"Id": "4"
}]
My Update request looks like this:
var request = new UpdateItemRequest {
TableName = "<table>",
Key = new Dictionary<string, AttributeValue> {{"Id", new AttributeValue("<item-id>")}},
UpdateExpression = "SET #a = #c.#a REMOVE #c",
ExpressionAttributeNames = new Dictionary<string, string> {
{"#c", "Data"},
{"#a", "Value"}
}
};
This works well for Id = 1 and 3. But does not work for 2 and 4. I assume because it can not do a SET. It does not throw any errors, but simply does not delete the Data attribute.
Is there a way to make it in a single call?
you just need a condition with your expression, to check if the attribute exist or not, thats it.
ConditionExpression ="attribute_exists (#c.#a)"
For more détails in Amazon doncs here.
I am trying to filter nested data but the catch is, the children I am also trying to filter are of a different type.
I have data that looks like this:
{
"value": [
{
"UserName": "scottketchum",
"FirstName": "Scott",
"LastName": "Ketchum",
"MiddleName": null,
"Gender": "Male",
"Age": null,
"Emails": [
"Scott#example.com"
],
"FavoriteFeature": "Feature1",
"Features": [],
"AddressInfo": [
{
"Address": "2817 Milton Dr.",
"City": {
"Name": "Albuquerque",
"CountryRegion": "United States",
"Region": "NM"
}
}
],
"HomeAddress": null
},
{
"UserName": "harryingram",
"FirstName": "Harry",
"LastName": "Ingram",
"MiddleName": null,
"Gender": "Male",
"Age": null,
"Emails": [
"Harry#example.com"
],
"FavoriteFeature": "Feature2",
"Features": [],
"AddressInfo": [
{
"Address": "123 Scott Ln.",
"City": {
"Name": "Nashville",
"CountryRegion": "United States",
"Region": "TN"
}
}
],
"HomeAddress": null
}
]
}
I need to be able to type the word "Scott" in my search field and return any person that has the name "Scott" or has an Address with the word "Scott" in it. So, ideally, the search would return both people.
void searchitemfromjson(string s)
{
var text = File.ReadAllText("D://jsontest.txt");
var jObject1 = JObject.Parse(text)["value"];
var searchs = new List<string>();
foreach (var item in jObject1)
{
var jObjitem = JObject.Parse(item.ToString());
IList<string> keys = jObjitem.Properties().Select(p => p.Name).ToList();
foreach (var k in keys)
{
if (k == "UserName")
{
if (jObjitem[k].ToString().Contains(s)) searchs.Add(jObjitem[k].ToString());
}
else if(k == "AddressInfo")
{
if(jObjitem["AddressInfo"][0]["Address"].ToString().Contains(s)) searchs.Add(jObjitem[k].ToString()); ;
}
}
}
}
I am fetching /me/apprequests from Facebook API. It works and I get the data in JSON format.
Thing is that everything is nested, like a dictionary inside a dictionary.
What i tried:
Dictionary<string, object> dict = Facebook.MiniJSON.Json.Deserialize(result.RawResult) as Dictionary<string, object>;
object data;
string request_code = "";
if (dict.TryGetValue("data", out data))
{
var rc = (((Dictionary<string, object>)data)["id"]);
request_code = (string)rc;
}
Debug.Log("request_code=" + request_code);
I think I need to loop the dictionary to get all id's.
I can confirm that if (dict.TryGetValue("data", out data)) does work correctly and get the dictionary of data arrays, but fails here (((Dictionary<string, object>)data)["id"]); with casting error.
Json looks like:
{
"data": [{
"application": {
"category": "Games",
"link": "https:\/\/www.facebook.com\/games\/?app_id=2523532533",
"name": "game name",
"id": "23432423423"
},
"created_time": "2019-02-27T16:01:15+0000",
"from": {
"name": "David boom",
"id": "387923432423089962"
},
"message": "You are invited",
"to": {
"name": "Dusty Spice",
"id": "10234324421033685"
},
"id": "413880842521239_10156578101000000"
},
{
"application": {
"category": "Games",
"link": "https:\/\/www.facebook.com\/games\/?app_id=2523532533",
"name": "game name",
"id": "23432423423"
},
"created_time": "2019-02-27T14:12:41+0000",
"from": {
"name": "David boom2",
"id": "387923432423089962"
},
"message": "You are invited",
"to": {
"name": "Dusty Spice",
"id": "10234324421033685"
},
"id": "316676422209302_10156578101000000"
}],
"paging": {
"cursors": {
"before": "NDEzODgwODQyNTIxMjM5Ojc1OTQzODY4NAZDZD",
"after": "MzE2Njc2NDIyMjA5MzAyOjc1OTQzODY4NAZDZD"
}
}
}
Managed to make it work if it will help someone:
string request_code = "";
if (dict.TryGetValue("data", out data))
{
int dataLength = ((List<object>)data).Count;
for (int i = 0; i < dataLength; i++)
{
var rc = ((List<object>)data)[i];
var rc2 = (((Dictionary<string, object>)rc)["id"]);
request_code = (string)rc2;
Debug.Log("request_code=" + request_code);
}
}
I am facing an issue while writing query to make a group by on inner list data to filter the outer list.
I have a collection structure like
"products"
{
"id": "97",
"name": "YI1",
"projects": [
{
"id": "92",
"name": "MUM",
"branches": [
{
"id": "62",
"name": "ON Service",
"geographyid": "84",
"geographyname": "North America",
"countryid": "52",
"countryname": "Canada"
}
],
"customers": [
{
"id": "80",
"name": "HEALTH SCIENCES"
}
]
}
],
},
"products"
{
"id": "96",
"name": "YI2",
"projects": [
{
"id": "94",
"name": "HHS",
"branches": [
{
"id": "64",
"name": "Hamilton ON Service",
"geographyid": "44",
"geographyname": "Asia",
"countryid": "58",
"countryname": "China"
}
],
"customers": [
{
"id": "40",
"name": "SCIENCES"
}
]
}
],
]
}
I am trying to have a new collection which can return an output as below
"Geography"{
"geographyid": "44",
"geographyname": "Asia",
"Country"
{
"countryid": "58",
"countryname": "China",
"branches"
{
"id": "94",
"name": "HHS
"customers"
{
"id": "40",
"name": "SCIENCES"
"projects"
{
"id": "94",
"name": "HHS",
"products"
{
"id": "96",
"name": "YI2",
}
}
},
}
}
},
"Geography"{
"geographyid": "84",
"geographyname": "North America"
"Country"
{
"countryid": "52",
"countryname": "Canada"
"branches"
{
"id": "62",
"name": "ON Service",
"customers"
{
"id": "80",
"name": "HEALTH SCIENCES"
"projects"
{
"id": "92",
"name": "MUM",
"products"
{
"id": "97",
"name": "YI1",
}
}
},
}
}
}
I tried multiple options and also write below query but I am still not getting required result.
var treeGroup = siteList.SelectMany(a => a.projects.Select(b => new { A = a, B = b }).ToList()).ToList()
.GroupBy(ol => new { ol.B.geographyid, ol.B.geographyname })
.Select(gGroup => new TreeNodes
{
id = gGroup.Key.geographyid,
name = gGroup.Key.geographyname,
type = Costants.geographyTreeNode,
parentid = string.Empty,
children = gGroup
.GroupBy(ol => new { ol.B.countryid, ol.B.countryname })
.Select(cGroup => new TreeNodes
{
id = cGroup.Key.countryid,
name = cGroup.Key.countryname,
type = Costants.countryTreeNode,
parentid = gGroup.Key.geographyid,
children = cGroup
.GroupBy(ol => new { ol.B.id, ol.B.name })
.Select(sGroup => new TreeNodes
{
id = sGroup.Key.id,
name = sGroup.Key.name,
type = Costants.branchTreeNode,
parentid = cGroup.Key.countryid,
children = sGroup
.Select(ol => new TreeNodes { id = ol.A.id, name = ol.A.name, type = Costants.siteTreeNode, parentid = sGroup.Key.id, children = new List<TreeNodes>() })
.ToList()
})
.ToList()
})
.ToList()
})
.ToList();
I can use looping logic to get the result, but I want to avoid it and try something with linq or lmbda expression.
I am able to resolve the issue. I took an additional parameters for Customers and then used the same with selectmany function on branches
The following code:
var data = _context.People.ToList(); //_context is my DataContext.
produces the result:
[{ "name": "john", "age": "30" }, { "name": "jane", "age": "31" }]
but, I want it to be a dictionary, so something like:
{ "xldata" : [{ "name": "john", "age": "30" }, { "name": "jane", "age": "31" }] }
I got it to work by doing:
Dictionary<string,List<People>> vals = new Dictionary<string, List<People>>();
vals.Add("xldata", people);
but, my dictionary's value is System.Object[] instead of the people
The purpose of this is to export data, so when I get to this line:
var people = jss.Deserialize<List<People>>(args["xldata"]);
args["xldata"] is `System.Object[]` and it says `Invalid JSON primitive`.
Here is the script is supposed to export the data to excel:
$.post(urlContent + exportHandlerPath, Json, function(data) {
var viewData = {};
viewData.xldata = JSON.stringify(data);
html = ich.excelExportTemplate(viewData);
$excelExportContainer.html(html);
var input = $excelExportContainer.find('input#excelExportHiddenField');
input.val(viewData.xldata);
var $excelForm = $('#excelExportForm');
$excelForm.attr('action', '/People/ExportToExcel/');
$excelForm.submit();
}
Apparently your args["xldata"] does not contain a json string like [{ "name": "john", "age": "30" }, { "name": "jane", "age": "31" }], but something returned by a .net object .ToString().
With JavaScriptSerializer.Deserialize you can only deserialize json represenations.