Get Values from a Dictionary in Python - python-3.x

I have a Database query returning objects as a list like this
table_query_response:
{
"Items":[
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2f4f826"
},
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2sfdgd"
}
],
"Count":2,
"ScannedCount":2,
"ResponseMetadata":{
"RequestId":"UEIQVN6HFB9K9Q20IE6J8L48CRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"HTTPStatusCode":200,
"HTTPHeaders":{
"server":"Server",
"date":"Thu, 10 Mar 2022 13:42:17 GMT",
"content-type":"application/x-amz-json-1.0",
"content-length":"151",
"connection":"keep-alive",
"x-amzn-requestid":"UEIQVN6HFB9K9Q20IE6J8L48CRVV4KQNSO5AEMVJF66Q9ASUAAJG",
"x-amz-crc32":"2870685196"
},
"RetryAttempts":0
}
}
and a different query to just get the specific items
group_query:
[
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2f4f826"
},
{
"groupId":"6501e5ac-59b2-4d05-810a-ee63d2sfdgd"
}
]
I want to get a list with values for the key groupid and store it like (6501e5ac-59b2-4d05-810a-ee63d2f4f826, 6501e5ac-59b2-4d05-810a-ee63d2sfdgd).
There can be more than two entries in that group query so I will need to get all the values for the groupid.

[x['groupId'] for x in table_query_response['Items']]

Related

How do I make a field path include a value in same document using MongoDB/pyMongo?

The documents in the MongoDB I am querying can be stripped down to this as an example:
{
"date":"2019-08-15",
"status":"5345",
"foo":
{
"bar":
{
"years":
{
"2018":
{
"const":1234
},
"2019":
{
"const":4321
}
}
}
}
}
I am trying to get the "const" values from this document using pyMongo.
The keys in "years" is varying with the "date" of the document.
I have attempted to use this pipeline where I try to use the year of "date" to get the "const" from this year:
pipeline=[
{'$match':{'status':{'$exists': True}}},
{'$project':
'const_thisYear':{
'$let':{
'vars':{
'yr':{ '$year': {'$convert':{'input': '$date','to': 'date'}}},
'res': '$foo.bar.years'
},
'in': '$$res.$$yr.const'
}
}
}
]
When aggregating I get the following python exception:
OperationFailure: FieldPath field names may not start with '$'.
How do I do this correctly?
Python 3.7.7
You should revise your collection structure to not store data as keys; but irrespective, just using regular python dict manipulation can get you out of the hole:
for doc in db.mycollection.find({'status': {'$exists': True}}, {'foo.bar.years': 1}):
for year, year_value in doc['foo']['bar']['years'].items():
print(year, year_value.get('const'))

How to define an index to use in a Mango Query

I am trying to create a CouchDB Mango Query with an index with the hope that the query runs faster. At the moment I have the following Mango Query which returns what I am looking for but it's slow. Therefore, I assume, I need to create an index to make it faster. I need help figuring out how to create that index.
selector: {
categoryIds: {
$in: categoryIds,
},
},
sort: [{ publicationDate: 'desc' }],
You can assume that my documents are let say news articles from different categories. Therefore in each document I have a field that contains one or more categories that the news article belongs to. For that I have an array of categoryIds for each document. My query needs to be optimized for queries like "Give me all news that have categoryId1 in their array of categoryIds sorted by publicationDate". What I don't know how to do is 1. How to define an index 2. What that index should be 3. How to use that index in "use_index" field of the Mango Query. Any help is appreciated.
Update after "Alexis Côté" answer:
If I define the index like this:
{
"_id": "_design/0f11ca4ef1ea06de05b31e6bd8265916c1bbe821",
"_rev": "6-adce50034e870aa02dc7e1e075c78361",
"language": "query",
"views": {
"categoryIds-json-index": {
"map": {
"fields": {
"categoryIds": "asc"
},
"partial_filter_selector": {}
},
"reduce": "_count",
"options": {
"def": {
"fields": [
"categoryIds"
]
}
}
}
}
}
And run the Mango Query like this:
{
"selector": {
"categoryIds": {
"$in": [
"e0bd5f97ac35bdf6893351337d269230"
]
}
},
"use_index": "categoryIds-json-index"
}
It still does return the results but they are not sorted in the order I want by publicationDate. So I am not clear what you are suggesting the solution is.
You can create an index as documented here
In your case, you will need an index on the "categoryIds" field.
You can specify the index using "use_index": "_design/<name>"
Note:The query planner should automatically pick this index if it's compatible.

How to match and join results between two resolvers in one graphql query?

I have two resolver.
The one is Company resolve that return the company details like id, name and list of documents ids, like this example:
{
"data": {
"companyOne": {
"name": "twitter",
"documents": [
"5c6c0213f0fa854bd7d4a38c",
"5c6c02948e0001a16529a1a1",
"5c6c02ee7e76c12075850119",
"5c6ef2ddd16e19889ffaffd0",
"5c72fb723ebf7b2881679ced",
"5c753d1c2e080fa4a2f86c87",
...
]
}
}
}
And the another resolver gets me all the details of documents like this example:
{
"data": {
"documentsMany": [{
"name": "doc1",
"_id": 5c6c0213f0fa854bd7d4a38c,
}, {
"name": "doc2",
"_id": 5c6c02948e0001a16529a1a1,
},
...
]
}
}
How to match every data.companyOne.documents[id] to data.documentsMany[..]._id? in the query level? is it possible to do this graphql?
The expect results should be when I run the companyOne query (without change the code - just in the query level) it's should return with documents as object instead of array of string ids.
maybe something like?
query {
companyOne {
name,
documents on documentsMany where _id is ___???
}
}

N1ql to Get data from collection based on field value

I have a document as below
{
"GMParcelStatus": {
"storeNumber": 5678,
"GMVehicleTrips": {
"GMVehicleTrip": [
{
"GMVehicleTripId": "1000101",
"MultiChannelOrders": {
"MultiChannelOrder": [
{
"multiChannelOrderID": "4BQGBNJ3U",
"multichannelParcels": [
{
"multiChannelParcelStatus": "LOADING_MISSING",
"UPI": "00000008101058629797"
},
{
"multiChannelParcelStatus": "OUTFORDELIVERY",
"UPI": "00000008101058684938"
}
]
},
{
"multiChannelOrderID": "4BQGUNY56W",
"multichannelParcels": [
{
"multiChannelParcelStatus": "DELIVERED",
"UPI": "00000008101058629793"
},
{
"multiChannelParcelStatus": "DELIVERED",
"UPI": "00000008101058684932"
}
]
}
]
}
}
]
}
},
"_class": "com.tesco.bean.MultiChannelParcelRequestVO"
}
I want to get all the document in my bucket data based on storeNumber and GMVehicleTripId.
I have 4 document similar to above with different GMVehicleTripId.
I have written N1ql query like below Select d.* from Delivery d JOIN Delivery.GMParcelStatus.GMVehicleTrips.GMVehicleTrip[0] b
on keys meta(d).id where b.GMVehicleTripId in ['1000101']
but i don't want to do this GMVehicleTrip[0].
please get me the right way to do.
Thanks,
Vinay J
SELECT d.* FROM Delivery d JOIN Delivery b ON KEYS meta(d).id
WHERE ANY v IN b.GMParcelStatus.GMVehicleTrips.GMVehicleTrip SATISFIES v.GMVehicleTripId IN ['1000101'] END;

Query the number of elements matching a filter using elastic.js?

I'm building a leaderboard with elasticsearch. I'd like to query all documents who have points greater than a given amount using the following query:
{
"constant_score" : {
"filter" : {
"range" : {
"totalPoints" : {
"gt": 242
}
}
}
}
This works perfectly -- elasticsearch appropriately returns all documents with points greater than 242. However, all I really need is the count of elements matching this query. Since I'm sending the result over the network, it would be helpful if the query simply returned the count, as opposed to all of the documents matching the filter.
How do I get elasticsearch to only report the count of documents matching the filter?
EDIT: I've learned that what I'm looking for is setting search_type to count. However, I'm not sure how to do this with elastic.js. Any noders willing to pitch in their advice?
You can use the query type count for exactly that purpose:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-search-type.html#count
This is an example that should help you:
GET /mymusic/itunes/_search?search_type=count
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"range": {
"year": {
"gt": 2000
}
}
}
}
}
}

Resources