foreach item in an array find largest record using mongodbcsharp driver - mongodb-.net-driver

Suppose there is an document like:
{
"_id": "1cf2080c-ce9a-367b-93ba-dbf7b2bf8a2c",
"template_id" : "1cf2080c-ce9a-367b-93ba-dbf7b2bf8a2c",
"some_value" : 12
}
mapping to a class as Template, and given a templateid array array_id = ["aa", "bb", "cc"]
question: how can I get a Template List using mongodb C# driver2.1 up, which each item in list have largest some_value ?
for example there is a collection:
{"_id": "1", "template_id": "aa", "some_value":1}
{"_id": "2", "template_id": "aa", "some_value":2}
{"_id": "3", "template_id": "aa", "some_value":3}
{"_id": "4", "template_id": "bb", "some_value":4}
{"_id": "5", "template_id": "bb", "some_value":5}
and a template_id_array = ["aa", "bb"]
expect result is:
{"_id": "3", "template_id": "aa", "some_value":3}
{"_id": "5", "template_id": "bb", "some_value":5}
I think it would be some "aggregate" or "map-reduce" like procedure, first match result and then group by template id and then find max somevalue for each group.

I have not tested this but should work with some probable modifications
var result = new List<Template>();
foreach (var element in template_id_array)
{
var filter = builder.Eq("TemplateId", element);
var doc = await collection.Find(filter)
.SortByDescending(x => x.SomeValue)
.Limit(1)
.FirstOrDefaultAsync();
result.Add(doc);
}
Then you can serialize it to the final form:
var expectedResult = JsonConvert.SerializeObject(result);

Related

Snaplogic - Expression Language Syntax - unique array value

I am using following EL
jsonPath($, "$array.map({id: value.get('id'), type: value.get('type') })")
which produces the next variable ...
But the key(id) is not kept unique ?!
[{
"id": "1",
"type": "1"
},
{
"id": "1",
"type": "2"
},
{
"id": "2",
"type": "1"
}]
What can i use in snaplogic expression language or a snap to get the following unique key array :
[{
"id": "1",
"types": ["1", "2"],
{
"id": "2",
"type": ["1"]
}]
Any ideas?
Use Group By Fields snap to group based on id and then use a simple mapper to create the desired JSON. Please note that you have to sort the incoming documents by id before doing the group by.
Sample Pipeline
Final Mapper expressions
$groupBy.id mapped to id
jsonPath($, "$groups[*].type") mapped to types
Resulting output

Update multiple columns of the table

when I update some columns of the table it insert new field in table
"id": 1,
"invoice_id": "0001-2019-20",
"client_id": "1",
"currency_id": "4",
"date": "2019-06-06",
"subject": "wordpress",
"total": "3000"
when i update only subject and total it will insert new field
"id": 2,
"invoice_id": "",
"client_id": "0",
"currency_id": "0",
"date": "0000-00-00",
"subject": "OMS",
"total": "0"
here is my controller,
$invoices = new Invoice;
$invoices->client_id = $request->get('client_id');
$invoices->currency_id = $request->get('currency_id');;
$invoices->subject = $request->get('subject');
$invoices->save();
The code you are using is for creating a new record.
However you are almost there.
So let's say you want to update below data
{"id": 1,
"invoice_id": "0001-2019-20",
"client_id": "1",
"currency_id": "4",
"date": "2019-06-06",
"subject": "wordpress",
"total": "3000"}
Use Below Code to update that row.
$invoices = Invoice::find($request->get('id'));
$invoices->client_id = $request->get('client_id');
$invoices->currency_id = $request->get('currency_id');
$invoices->subject = $request->get('subject');
$invoices->save();
There are alternative method as below.
$invoices = Invoice::find($request->get('id'));
$invoices->update([
'client_id' => $request->get('client_id'),
'currency_id' => $request->get('currency_id'),
'subject' => $request->get('subject')
]);
Note: For above method array key names and your table column names
must be same.

Pyes search on particular field gives no results

I have a document indexed whose structure is as follows:
{
"_index": "yelp",
"_type": "user",
"_id": "ABC",
"_score": 1,
"_source": {
"yelping_since": "2007-07",
"votes": {},
"review_count": 1798,
"name": "TEST_USER",
"user_id": "123",
"friends": [],
"fans": 89,
"average_stars": 3.48,
"type": "user",
"compliments": {},
"elite": []
}
}
When I do:
myFilter = TermFilter("_id", "ABC")
q = FilteredQuery(MatchAllQuery(), myFilter).search()
results = conn.search(query=q, indices=index_name,type=doc_typeU)
Exact result is displayed.
But when I try to search for field say user_id as follows,
myFilter = TermFilter("user_id", "123")
q = FilteredQuery(MatchAllQuery(), myFilter).search()
results = conn.search(query=q, indices=index_name,type=doc_typeU)
No results are displayed. Any idea why?
please try this one
myFilter = TermQuery("user_id", "123")
q = FilteredQuery(MatchAllQuery(), myFilter).search()
results = conn.search(query=q, indices=index_name,type=doc_typeU)

Set Reference Points for Overlaying Data onto an image in R

I have seen a few questions on here (one was even by me) with respect to using R to plot an image.
The difference here is that I need to set the reference cooridnates for my image to match the data I am looking to plot on top of the image.
More specifically, I need R to understand that the coordinates for the background image are x = (-100,100) and y = (40,-40).
I have been able to to read in the image file and plot it using the ReadImages package, but when I overlay my data using points(), the data obviously do not line up appropriately.
Any help is much appreciated.
EDIT: here are some example data and I attached the image:
structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"), class = "data.frame", row.names = c(74328L,
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
100163L, 100167L))
You can create an empty plot with the correct dimensions, then use the rasterImage function to plot the image, then adding the points should work fine.
Another approach is to use the updateusr function from the TeachingDemos package after plotting the image to make sure that the coordinates match what you want them to before adding lines or points.
I saved your graphic above as ice.png and ran the following code:
library(EBImage)
ice <- readImage('My Pictures/ice.png')
pos <- structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"),
class = "data.frame", row.names = c(74328L,
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
100163L, 100167L))
pos$xcoord <- as.numeric(pos$xcoord)
pos$ycoord <- as.numeric(pos$ycoord)
ice2 <- as.raster(ice)
pin <- par('pin')
plot( c(-100,100), c(-40,40), type='n', xlab='', ylab='',
asp=pin[1]/pin[2], axes=FALSE, xaxs='i', yaxs='i')
rasterImage(ice2, -100, -40, 100, 40, interpolate=FALSE)
with(pos, text(xcoord, ycoord, teamid, col='green', cex=1.2) )
Does this do what you want?

CouchDB complex key view not returning results as expected

I am using CouhDB 1.1 - I am trying to do a:
SELECT doc.data WHERE doc._id="c" order by doc.date
I have the following 3 docs:
{
"_id": "c",
"_rev": "7-4193c63ab034214ff5514006ac1143b2",
"data": "c",
"date": "2009/01/01"
}
{
"_id": "b",
"_rev": "3-9200b985d55a8f778d9726d6f1e9dbe0",
"data": "b",
"date": "2010/01/01"
}
{
"_id": "a",
"_rev": "4-2834579ebc10e0467191bf9c8f115b60",
"data": "a",
"date": "2011/01/01"
}
And the following view to query it:
function(doc) {
emit([doc.data, doc.date], doc._id);
}
I am sending the following query:
http://localhost:5984/tryme/_design/view/_view/data?key=[{},"c"]&descending=true
which returns 0 docs
and
http://localhost:5984/tryme/_design/view/_view/data?startkey=[{},"c"]&descending=true
which returns all docs...
I am assuming this should work, based on this: translate this mysql into a couchdb view?
Thanks
- Sami

Resources