Flattening af collection to properties on document in RavenDB - ravendb

I have some documents in RavenDB with a collection of attributes, which I would like to flatten using an index.
The document structure is something similar to:
{
"Id": "test/1",
"Name": "Some name",
"Attributes": [
{ "Key": "FirstAttr", "Value": "FirstValue" },
{ "Key": "SecondAttr", "Value": "SecondValue" }
]}
My desired output is:
{
"Id": "test/1",
"Name": "Some name",
"FirstAttr": "FirstValue",
"SecondAttr": "SecondValue"
}
Is this possible in RavenDB?
Thanks a bunch!

Ok - answering my own question so others can benefit:
The CreateField method seems to be what I need in the map part in my index:
from t in docs.Test
select new {
t.Id,
t.Name,
_ = t.Attributes.Select(x => this.CreateField(x.Key, x.Value, true, true))
}

Can you do this during the map-reduce?
Map = docs =>
from doc in docs
select new {
Id = doc.Id,
Name = doc.Name,
FirstAttr = (doc.Attributes.ContainsKey("FirstAttr") ? doc.Attributes["FirstAttr"] : null,
SecondAttr = (doc.Attributes.ContainsKey("SecondAttr") ? doc.Attributes["SecondAttr"] : null
};

Related

data.tables is empty - community visualization in DataStudio

I'm trying to draw custom graph with Google DataStudio community visualization and BigQuery source.
But even if data is exist and valid(check with other basic chart), input data of my drawViz function is empty.
See below Javascript code:
function drawViz(data) {
let rowData = data.tables.DEFAULT;
var metricName = data.fields['barMetric'][0].name;
var dimensionName = data.fields['barDimension'][0].name;
title = metricName + ' by ' + dimensionName + '2';
console.log(rowData , title )
}
Console output:
> {DEFAULT : Array(0)} "my metrics by my dimension"
Is there any restriction using community visualization functionality with bigquery?
Or need any additional setting except in codelab (https://codelabs.developers.google.com/codelabs/community-visualization/#0) ?
** update
manifest.json : https://storage.googleapis.com/vd-qoe-bucket/test/manifest.json
myViz.json : https://storage.googleapis.com/vd-qoe-bucket/test/myViz.json
From your links:
The "data" part of your config appears to be invalid:
"data": [
{
"id": "concepts",
"label": "Concepts",
"elements": [ // setting metric and dimension counts
{
"id": "barDimension",
"label": "Dimension",
"type": "DIMENSION",
"options": {
"min": 1,
"max": 2
}
},
{
"id": "barMetric",
"label": "Metric",
"type": "METRIC",
"options": {
"min": 1,
"max": 2
}
}
]
}
]
Removing the comment // setting dimensions... should work.

Extract data from json array in Karate

In the below JSON response, I need to extract the 'cid' for the record that has the 'nationalityDecription' as 'USA'. By using this query as a reference, I used the below loc in the karate feature file, but 1st line itself fails with syntax error(tried different combinations). For now, I'm using the custom javascript as a workaround which is working fine. I need help to check if i'm missing anything in syntax. Thanks
Response:
{
"header": {
"Id": "12345678",
"timeStamp": "2018-09-17T10:09:812.000"
},
"dataRecords": [
{
"cid": "31H678",
"cidMeta": "00",
"nationalityDecription": "CHINA"
},
{
"cid": "31S421",
"cidMeta": "01",
"nationalityDecription": "USA"
}
]
}
Feature file:
* def record= $response.dataRecords[?(#.nationalityDecription=='USA')]
* def cid = record.cid
* def response = { "header": { "Id": "12345678", "timeStamp": "2018-09-17T10:09:812.000" }, "dataRecords": [ { "cid": "31H678", "cidMeta": "00", "nationalityDecription": "CHINA" }, { "cid": "31S421", "cidMeta": "01", "nationalityDecription": "USA" } ] }
* def cid = get[0] response.dataRecords[?(#.nationalityDecription=='USA')].cid
* match cid == '31S421'

Nested query not working in Cosmos DB more than 1 level deep

I can't get Cosmos DB to find a record more than 1 level deep in a query:
Document:
{
"CustomerId": 1,
"Orders": [
{
"OrderId": 1,
"OrderCode": "ONE",
"OrderName": "Order One",
"Products": [
{
"ProductCode": "ONE",
"ProductName": "Product One"
}
]
}
]
}
Query: db.customers.find({ "Orders.Products.ProductCode": "ONE" })
It works in MongoDB shell, but not in CosmosDB. Am I missing something obvious?
I think the key of the issue is that there are nested arrays in your document, and the query you provide may only apply to the following document:
"CustomerId" : 1,
"Orders" : {
"OrderId" : 1,
"OrderCode" : "ONE",
"OrderName" : "Order One",
"Products" : {
"ProductCode" : "ONE",
"ProductName" : "Product One"
}
}
You could refer to MongoDB official documentation and use $elemmatch operator to query fields in nested documents.
query:
{ "Orders": { $elemMatch: { Products : {$elemMatch : {ProductCode : "ONE" } } } } }
Hope it helps you.
Cosmosdb does not have something to query in nested document. you need to use ARRAY_CONTAINS built-in function. So it will be something like this:
SELECT c.Orders.Products.ProductCode, c.Orders.Products.ProductName
FROM c
WHERE c.Orders.Products.ProductCode = "ONE" or ARRAY_CONTAINS(c.Orders.Products, { "ProductCode": "ONE" })

React Native -- Loop through array of objects to return value

Data:
{
"contextTag": {
"value": "Bittersweet",
"valueLabel": "Bittersweet"
},
"tags": [
{
"name": "tag",
"value": "Creamy"
},
{
"name": "tag",
"value": "Colorful"
},
{
"name": "tag",
"value": "Bright"
}
],
"rating": 5,
"userNickName": "HelloGames",
"userLocation": "UK",
"title": "Great!",
"reviewText": "Yada yada yada yada",
"submissionTime": "30 Nov 16"
},
I currently have this working for getting contextTag valueLabels:
this.props.reviewData.reviews.map(
(o) => {
return o.contextTag && o.contextTag.valueLabel ? o.contextTag.valueLabel.trim() : '';
}
)
And this for tags:
this.props.reviewData.reviews.map(
(o) => {
return o.tags && o.tags.value ? o.tags.value.trim() : '';
}
)
But it's coming back empty. How do I loop through tags to grab each of the values?
You can cache the tags, then map over it to get the values. Like below:
const tags = this.props.reviewData.reviews.tags;
const tags_values = ( tags ? tags.map((tag) => (tag.value ? tag.value : '' ) : []); // this an array of the tags values.
Your code does not return what you want because the tags attributes is an array of objects, so to get the tag values you have to map over it as I did above.
Hope this helped.

make a new array from a nested object using Lodash

Here is my data
[
{
"properties": {
"key": {
"data": "companya data",
"company": "Company A"
}
},
"uniqueId" : 1
},
{
"properties": {
"key": {
"data": "companyb data",
"company": "Company B"
}
},
"uniqueId" : 2
},
{
"properties": {
"key": {
"data": "companyc data",
"company": "Company C"
}
},
"uniqueId" : 3
}
]
The format I need for my typeahead directive is below. I was trying to figure out the other post I made but still couldn't make it work. The best was to just make the nested collection as a simple collection of object.
[
{
"uniqueId" : 1,
"data": "companya data"
},
{
"uniqueId" : 2,
"data": "companyb data"
},
{
"uniqueId" : 3,
"data": "companyc data"
}
]
I got it!
console.log(
_(jsonData).map(function(obj) {
return {
d : obj.properties.key.data,
id : obj.uniqueId
}
})
.value()
);
You do not have to use the chaining feature of lodash as long as you are only performing one operation. You can simply use:
_.map(jsonData, function(obj) {
return {
d : obj.properties.key.data,
id : obj.uniqueId
}
});