MongoDB min query - mongodb-query

I am fighting bit with min query in the MongoDB. Probably that's not the correct way how to get expected result, but please, help me what am I doing wrong.
Let's assume we have such a data:
{ "version": 1, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 1, "id": "789-987", "type":"ChangeEvent", "data":"aaa" }
{ "version": 2, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 2, "id": "789-987", "type":"ChangeEvent", "data":"aaa" }
{ "version": 3, "id": "123-456", "type":"AnotherEvent", "data":"aaa" }
{ "version": 4, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 5, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 6, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 7, "id": "123-456", "type":"AnotherEvent", "data":"aaa" }
{ "version": 8, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 3, "id": "789-987", "type":"ChangeEvent", "data":"aaa" }
{ "version": 9, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
So I have two different ids, and two different types.
What I need to receive is only id "123-456" data from latest "AnotherEvent" including.
That would mean,
{ "version": 7, "id": "123-456", "type":"AnotherEvent", "data":"aaa" }
{ "version": 8, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
{ "version": 9, "id": "123-456", "type":"ChangeEvent", "data":"aaa" }
I thought that using min cursor will help me, concretely this query:
db.data.find({id: '123-456'}).max( { id: '123-456', type: 'AnotherEvent' } ).hint( { id: 1, type: 1 }).pretty()
Is this event possible to do directly on the Mongo side through the query?
Thanks a lot.

Related

Fetching for Strapi v3 is not working with Strapi v4

With Strapi v3 my code perfectly working:
api.loadSource(async actions => {
const { data } = await axios.get('http://localhost:1337/events')
const collection = actions.addCollection({
typeName: 'Event',
path: '/events/:id'
})
for (const event of data) {
collection.addNode({
id: event.id,
})
}
})
But with update to Strapi v4 I'm getting the error: TypeError: gridsome.server.js: data is not iterable (29:25)
27 | })
28 |
> 29 | for (const event of data) {
| ^
30 | collection.addNode({
31 | id: event.id,
I know, that now Strapi added /api/ to the path and also not showing relations by default. But this not fixing the situation:
const { data } = await axios.get('http://localhost:1337/api/events?populate=')
output when enter in browser http://localhost:1337/api/events/1
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z"
}
},
"meta": {
}
}
When enter http://localhost:1337/api/events/1?populate=*
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z",
"categories": {
"data": [
{
"id": 1,
"attributes": {
"name": "Music",
"createdAt": "2022-06-11T12:44:07.472Z",
"updatedAt": "2022-06-11T12:45:32.721Z",
"publishedAt": "2022-06-11T12:45:32.720Z"
}
}
]
}
}
},
"meta": {
}
}

what will be the query for nested object filtration in sql for json field data

passing ID = 14385 in where condition and want return that object specific key from json document.
i want matched object key value like name: "Binod"
when ID matched with 14837 then return Sunil
jsonField value::
{
"nodeInfo":
{
"9":
{
"1": { "ID": "14835", "name": "Binod" },
"2": { "ID": "14836", "name": "Rahul" },
"3": { "ID": "14837", "name": "Sunil" },
},
"10":
{
"2": { "ID": "14838", "name": "Shikhar" },
"5": { "ID": "14839", "name": "Hari" },
"6": { "ID": "14840", "name": "Harsh" },
},
"7":
{
"cityData": {
"1": { "ID": "14855", "name": "Gannu" },
"2": { "ID": "14856", "name": "Singh" }
},
"townData": {
"5": { "ID": "14857", "name": "Bhusan" },
"7": { "ID": "14858", "name": "Ashwani" }
},
"areaData": {
"9": { "ID": "14861", "name": "Anjali" },
"1": { "ID": "14862", "name": "Pari Chauhan" }
},
}
}
}

Grouping Response Data

I am trying to get data from my database but when I show and make it become api response, I have some problem for grouping it based on productid.
I have response data that created based on golang like this:
[
{
"product_id": "1",
"product_name": "Cardigan",
"pitems": [
{
"id": "625ad1bc-66c5-440e-a527-d029d401ec2b",
"name": "Box",
"qty": 1
},
{
"id": "625ad1bc-66c6-440e-a527-d029d401ec2b",
"name": "test items1",
"qty": 1
},
{
"id": "625ad1bc-66c7-440e-a527-d029d401ec2b",
"name": "test items2",
"qty": 1
},
{
"id": "625ad1bc-66c8-440e-a527-d029d401ec2b",
"name": "test items3",
"qty": 1
}
]
},
{
"product_id": "2",
"product_name": "Polo",
"product_sku": "P01",
"items": [
{
"id": "625ad1bc-66c5-440e-a527-d029d401ec2b",
"name": "Box",
"qty": 1
},
{
"id": "625ad1bc-66c6-440e-a527-d029d401ec2b",
"name": "test items1",
"qty": 1
},
{
"id": "625ad1bc-66c7-440e-a527-d029d401ec2b",
"name": "test items2",
"qty": 1
},
{
"id": "625ad1bc-66c8-440e-a527-d029d401ec2b",
"name": "test items3",
"qty": 1
}
]
}
]
But This response is not my expected result, my expected result is like:
[
{
"product_id": "1",
"product_name": "Cardigan",
"pitems": [
{
"id": "625ad1bc-66c5-440e-a527-d029d401ec2b",
"name": "Box",
"qty": 1
},
{
"id": "625ad1bc-66c6-440e-a527-d029d401ec2b",
"name": "test items1",
"qty": 1
},
{
"id": "625ad1bc-66c7-440e-a527-d029d401ec2b",
"name": "test items2",
"qty": 1
}
]
},
{
"product_id": "2",
"product_name": "Polo",
"product_sku": "P01",
"items": [
{
"id": "625ad1bc-66c8-440e-a527-d029d401ec2b",
"name": "test items3",
"qty": 1
}
]
}
]
Can Anyone help me to solve my problem?
What does it mean to show detailed data?
the easy way is:
create 2 func like this:
func detail(id int)(result model.Struct)
{ return result }
func product()(result model.Struct_Result) {
for data.Next() {
// call func detail
data.Scan(&id, &product)
detailResult := detail(id)
// then put together with struct and mix append ()
outputLoop := model.Result{
"product_id": id,
"pitems": [
{
"id": detailResult.id,
"name": detailResult.name,
"qty": detailResult.qty
},
]
}
result = append(result,outputLoop)
}
return result
}

Query in Elastic Search with some field and some condition?

I have data about Product, which have some fields ( _id, Shop, ProductVerion...). It has indexed in Elastic Search. And I want to search products have max ProductVersion with Shop.
Ex:
Shop Amazon has 3 Version crawl product: 111,222,333.
Shop Ebay has 2 version: 222,444
Shop Alibaba has 2 version: 111, 444
Verions may may be the same.
And now, I want to get Products which have:
Shop Amazon and ProducVersion 333
or Shop Ebay and ProductVersion 444
or Shop Alibaba and ProductVersion 444.
But I don't know query that.
Help me, pls!!
I tried it out with some example documents. I kept version field as numeric field.
These are example documents with which I tried
[
{
"_index": "test",
"_type": "doc",
"_id": "12334",
"_score": 1,
"_source": {
"shopName": "amazon",
"version": 341
}
},
{
"_index": "test",
"_type": "doc",
"_id": "123",
"_score": 1,
"_source": {
"shopName": "amazon",
"version": 3412
}
},
{
"_index": "test",
"_type": "doc",
"_id": "1233",
"_score": 1,
"_source": {
"shopName": "amazon",
"version": 341
}
},
{
"_index": "test",
"_type": "doc",
"_id": "1238",
"_score": 1,
"_source": {
"shopName": "alibaba",
"version": 34120
}
},
{
"_index": "test",
"_type": "doc",
"_id": "1239",
"_score": 1,
"_source": {
"shopName": "alibaba",
"version": 3414
}
},
{
"_index": "test",
"_type": "doc",
"_id": "123910",
"_score": 1,
"_source": {
"shopName": "alibaba",
"version": 124
}
}
]
As #demas had specified I went ahead with terms aggregation and top hits aggregation
indexName/_search
{
"size": 0,
"aggs": {
"shop": {
"terms": {
"field": "shopName.keyword"
},
"aggs": {
"product": {
"top_hits": {
"size": 1,
"sort": [
{
"version": {
"order": "desc"
}
}
]
}
}
}
}
}
}
This should provide you with the document that contains the highest product version number for each shop as shown below.
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"shop": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "alibaba",
"doc_count": 3,
"product": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "1238",
"_score": null,
"_source": {
"shopName": "alibaba",
"version": 34120
},
"sort": [
34120
]
}
]
}
}
},
{
"key": "amazon",
"doc_count": 3,
"product": {
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "123",
"_score": null,
"_source": {
"shopName": "amazon",
"version": 3412
},
"sort": [
3412
]
}
]
}
}
}
]
}
}
}
It will be better if you provide reproducible example, but it looks like you need to use aggregation framework: term aggregation and top hits aggregation:
GET /_search
{
"aggs": {
"shops": {
"terms": { "field": "Shop" }
},
"aggs": {
"tops": {
"top_hits": {
"size": 100
}
}
}
}
}

elasticsearch multi-index get request does not work

How can I use GET api on multiple indexes?
I tried the following but I keep getting index missing exception.
http://localhost:9200/index1,index2/_all/AUy25vKhcC3G2n2ukra3
Output:
{
"error" : "IndexMissingException[[index1,index2] missing]",
"status" : 404
}
Please help
Use multi-get api
Example:
POST test1/index1/1
{
"name": "jon brick"
}
POST test2/index1/1
{
"name": "sriji"
}
GET _mget
{
"docs" : [
{
"_index": "test1",
"_type":"index1",
"_id" : "1"
},
{
"_index": "test2",
"_type":"index1",
"_id" : "1"
}
]
}
and the result is:
{
"docs": [
{
"_index": "test1",
"_type": "index1",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "jon brick"
}
},
{
"_index": "test2",
"_type": "index1",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "sriji"
}
}
]
}