Exclude a certian field or make it Null inside a AQL query - arangodb

I have problem. I have a collection orders. I want to get all documents and all field except the field dataOriginSystem inside metaData.
Is there an option to exclude this value, or make it null?
SELECT * EXCEPT(ColumnNameX, [ColumnNameY, ...])
FROM TableA
This my collection orders:
[
{'_id': 'orders/213123',
'contactEditor': {'name': 'Max Power',
'phone': '1234567',
'email': 'max#power.com'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': 'maxnot#power.com'},
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': 'Goods',
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
{'_id': 'orders/12323',
'contactEditor': {'name': 'Max Power2',
'phone': '1234567',
'email': 'max#power.com'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': 'maxnot#power.com'},
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': 'Goods',
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
]
AQL
FOR doc IN orders RETURN doc
I want something like
FOR doc IN orders RETURN doc WITHOUT metaData_dataOriginSystem
FOR doc IN orders RETURN doc AND metaData_dataOriginSystem = NULL
So that the returned documents do not have the field metaData_dataOriginSystem or the field should be Null
What I want (the other fields should all exist!)
[
{'_id': 'orders/213123',
...
'isCompleteDelivery': False,
'metaData': {<Removed>,
'dataOriginWasCreatedTime': '10:12:12',},
...
},
OR
{'_id': 'orders/12323',
...
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': Null,
'dataOriginWasCreatedTime': '10:12:12',},
...
},
]

Use UNSET() or UNSET_RECURSIVE().
UNSET(document, attributeName1, attributeName2, ... attributeNameN) → doc
Remove the attributes attributeName1 to attributeNameN from document. All other attributes will be preserved.
FOR doc IN orders RETURN UNSET( doc, "orderData", "dataOriginSystem")
If you want to remove a nested value, you need to Merge
FOR doc IN orders RETURN MERGE(doc, {metaData: UNSET(doc.metaData, "dataOriginSystem")})

Related

Flatterning JSON to Pandas Dataframe

I'm trying to flattern this json into a pandas dataframe, but it's getting the better of me.
[{
'contact': {
'id': 101,
'email': 'email1#address.com',
},
'marketingPreference': [{
'marketingId': 1093,
'isOptedIn': True,
'dateModifed': '2022-05-10T14:29:24Z'
}]
},
{
'contact': {
'id': 102,
'email': 'email2#address.com',
},
'marketingPreference': [{
'marketingId': 1093,
'isOptedIn': True,
'dateModifed': '2022-05-10T14:29:24Z'
}]
}
]
I am looking for the columns to be: Id, Email, MarketingId, IsOptedIn, DateModifed.
Even though marketingPreference is an array, there is only ever one json object inside.
You can use pd.json_normalize
df = pd.json_normalize(data, record_path='marketingPreference', meta=[['contact', 'id'], ['contact', 'email']])
print(df)
marketingId isOptedIn dateModifed contact.id contact.email
0 1093 True 2022-05-10T14:29:24Z 101 email1#address.com
1 1093 True 2022-05-10T14:29:24Z 102 email2#address.com

Python Marshmallow AttributeError: 'list' object has no attribute 'get'

I have this schema
from marshmallow import validate, ValidationError
from marshmallow_jsonapi import fields
from marshmallow_jsonapi.flask import Relationship, Schema
class UserSchema(Schema):
first_name = fields.Str(required=True])
last_name = fields.Str(required=True)
title = fields.Str(required=True)
class Meta:
type_ = 'users'
self_view = "blog_view.users_detail"
self_view_kwargs = {"user_id": "<id>", "_external": True}
self_view_many = "blog_view.users_list"
blog= Relationship(
many=False,
include_data=True,
type_="blogs",
include_resource_linkage=True,
schema="BlogSchema"
)
I want to load this data(coming from UI) for validation:
bulk_data = [
{ 'type': 'users',
'relationships': {'blog': {'data': {'type': 'blogs', 'id': blog_id}}},
{'first_name': 'Billy', 'last_name': 'Butcher', 'title': 'Supe Hunter'}
},
{ 'type': 'users',
'relationships': {'blog': {'data': {'type': 'blogs', 'id': blog_id}}},
{'first_name': 'Home', 'last_name': 'Lander', 'title': 'Leader'}
},
{ 'type': 'users',
'relationships': {'blog': {'data': {'type': 'blogs', 'id': blog_id}}},
{'first_name': 'Black', 'last_name': 'Noir', 'title': 'Super Ninja'}
}
]
For validation I did:
data = UserSchema(many=True).load(input_data)
I get an error saying,
AttributeError: 'list' object has no attribute 'get'
which is obvious because I'm passing a list. The validation works fine when I pass a single dictionary from the above list, but I want to pass the bulk data and do validation at once as shown in Marshmallow doc: https://marshmallow.readthedocs.io/en/stable/quickstart.html#validation
When
many=True
, load method expects a collection type so list, tuple, queryset etc.
Any suggestion on how to validate a list of data in Marshmallow? The marshmallow versions are:
marshmallow==2.18.0
marshmallow-jsonapi==0.23.1
Thanks!

Spotipy - Listing only track and artists names in a playlist

Hello All and thank you in advance for your help :)
Can someone help me understand how I can take the below code, which displays data for a specified playlist, and have it only show the artist and track names? I have been toying around with the API documentation for several hours and I have not been able to make heads or tales of it. Right now when it displays data it gives me a whole bunch of data in a jumbled mess. Also, note that I put dummy values in the client_id and Secret parts of this code.
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import json
PlaylistExample = '37i9dQZEVXbMDoHDwVN2tF'
cid = '123'
secret = 'xyz'
auth_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
playlist_id = 'spotify:user:spotifycharts:playlist:37i9dQZEVXbJiZcmkrIHGU'
results = sp.playlist(playlist_id)
print(json.dumps(results, indent=4))
Would something like this be useful?:
print("Song - Artist - Album\n")
for item in results['tracks']['items']:
print(
item['track']['name'] + ' - ' +
item['track']['artists'][0]['name'] + ' - ' +
item['track']['album']['name']
)
Your output will look similar to this:
Song - Artist - Album
ONLY - ZHU - ONLY
Bad - 2012 Remaster - Michael Jackson - Bad 25th Anniversary
Orion - Rodrigo y Gabriela - Rodrigo y Gabriela
Shape of You - Ed Sheeran - ÷ (Deluxe)
Alternatively, you could create your own structure based on the returned one by Spotify but just keeping what you need:
result_dict = {
'tracks': {
'items': [],
'limit': 100,
'next': None,
'offset': 0,
'previous': None,
'total': 16
},
'type': 'playlist',
'uri': '<playlist_uri>'
}
And your track structure that goes inside 'items' from above:
track_dict = {
'track': {
'album': {
'name': item['track']['album']['name'],
},
'artists': [{
'name': item['track']['artists'][0]['name'],
}],
'name': item['track']['name'],
}
}
Then iterate and insert one by one:
for item in results['tracks']['items']:
track_dict = {
'track': {
'album': {
'name': item['track']['album']['name'],
},
'artists': [{
'name': item['track']['artists'][0]['name'],
}],
'name': item['track']['name'],
}
}
# Append the track dict structure to your results dict structure
result_dict['tracks']['items'].append(track_dict)
Having this as a result when printing result_dict:
{
'tracks': {
'items': [{
'track': {
'album': {
'name': 'ONLY'
},
'artists': [{
'name': 'ZHU'
}],
'name': 'ONLY'
}
}, {
'track': {
'album': {
'name': 'Bad 25th Anniversary'
},
'artists': [{
'name': 'Michael Jackson'
}],
'name': 'Bad - 2012 Remaster'
}
}, {
'track': {
'album': {
'name': 'Rodrigo y Gabriela'
},
'artists': [{
'name': 'Rodrigo y Gabriela'
}],
'name': 'Orion'
}
}, {
'track': {
'album': {
'name': '÷ (Deluxe)'
},
'artists': [{
'name': 'Ed Sheeran'
}],
'name': 'Shape of You'
}
}],
'limit': 100,
'next': None,
'offset': 0,
'previous': None,
'total': 4
},
'type': 'playlist',
'uri': '<playlist_uri>'
}

How to disable Adaptive Cards on clicking once?

I am using an adaptive card in MSTeams Bot and on clicking once I want to disable the Submit button to prevent the user from clicking it again as the backend is running for the button click event.
Adaptive Card code -
async specialRewards() {
const specialRewardCard = CardFactory.adaptiveCard({
'$schema': 'http://adaptivecards.io/schemas/adaptive-card.json',
'version': '1.2',
'type': 'AdaptiveCard',
'body': [
{
'type': 'TextBlock',
'text': "Hey there! \n\n",
'wrap': true,
},
{
'type': 'TextBlock',
'text': 'Your birthday🎂 :',
'weight': 'Bolder',
'wrap': true,
},
{
'type': 'Input.Date',
'id': 'birthday',
'placeholder': 'Enter a date',
'spacing': 'Padding',
},
{
'type': 'TextBlock',
'text': 'Your work anniversary🎉 :',
'weight': 'Bolder',
'wrap': true,
},
{
'type': 'Input.Date',
'id': 'anniversary',
'placeholder': 'Enter a date',
'spacing': 'Padding',
},
],
'actions': [
{
'type': 'Action.Submit',
'title': 'Submit',
'isPrimary': true,
},
],
});
return specialRewardCard;
}
This is how it is looking on MSTeams
I'm working on a similar scenario myself at the moment and I've found the updateActivity() function to work well.
// Update the adaptive card so it cannot be used again
async followUp() {
const card = CardFactory.heroCard(
'Your card results',
'<b>Birthday:</b> ' + birthday + '<br>' + '<b>Anniversary:</b> ' + anniversary,
null
);
card.id = step.context.activity.replyToId;
const message = MessageFactory.attachment(card);
message.id = step.context.activity.replyToId;
await step.context.updateActivity(message);
}

Add new fields to specific collection element in MongoDB pymongo

I am trying to update existing array element by adding new fields into it.
...
{
"_id": "f08b466a-163b-4d9e-98f5-d900ef0f1a26",
"firstName": "foo",
"result": [
{
"_id":"957ee97d-d461-4d6c-8a80-57351bdc29f7",
"subjectName":"Mathematics",
"marks": 60
},
{
"_id":"0591d9a0-fd0f-4876-9bd3-dec4d5ab452e",
"subjectName":"Science",
"marks": 70
},
{
"_id":"21f42104-791b-4522-81ce-f7ae1b30d075",
"subjectName":"Social science",
"marks": 50
}
]
},
...
Now I want to add new field to science subject called "isFavorite: true"
like,
{
"_id": "f08b466a-163b-4d9e-98f5-d900ef0f1a26",
"firstName": "foo",
"result": [
{
"_id":"957ee97d-d461-4d6c-8a80-57351bdc29f7",
"subjectName":"Mathematics",
"marks": 60
},
{
"_id":"0591d9a0-fd0f-4876-9bd3-dec4d5ab452e",
"subjectName":"Science",
"marks": 70
"isFavorite": true #-----------------New field----------
},
{
"_id":"21f42104-791b-4522-81ce-f7ae1b30d075",
"subjectName":"Social science",
"marks": 50
}
]
},
...
What I tried so far?
from pymongo import MongoClient
...
collection = mongoInstance["student"]
student = collection.find_one({"_id": "f08b466a-163b-4d9e-98f5-d900ef0f1a26"})
for result in student["result"]:
if result["_id"] == "0591d9a0-fd0f-4876-9bd3-dec4d5ab452e":
result["isFavorite"] = True
break
collection.update_one({"_id": "f08b466a-163b-4d9e-98f5-d900ef0f1a26"}, {"$set": student })
This is working, but I believe there might be simple way to just find student document by it's id and adding new field to array item by item._id.
Looking for some elegant Mongodb query to find and update specific array element.
#Alex Blex was on the right lines regarding the positional operator; the pymongo syntax is very similar:
db.mycollection.update_one({'_id': 'f08b466a-163b-4d9e-98f5-d900ef0f1a26',
'result._id': '0591d9a0-fd0f-4876-9bd3-dec4d5ab452e'},
{'$set': {'result.$.isFavorite': True}})
Full example using the sample data provided:
from pymongo import MongoClient
import pprint
db = MongoClient()['mydatabase']
db.mycollection.insert_one({
'_id': 'f08b466a-163b-4d9e-98f5-d900ef0f1a26',
'firstName': 'foo',
'result': [
{
'_id': '957ee97d-d461-4d6c-8a80-57351bdc29f7',
'subjectName': 'Mathematics',
'marks': 60
},
{
'_id': '0591d9a0-fd0f-4876-9bd3-dec4d5ab452e',
'subjectName': 'Science',
'marks': 70
},
{
'_id': '21f42104-791b-4522-81ce-f7ae1b30d075',
'subjectName': 'Social science',
'marks': 50
}
]
})
db.mycollection.update_one({'_id': 'f08b466a-163b-4d9e-98f5-d900ef0f1a26',
'result._id': '0591d9a0-fd0f-4876-9bd3-dec4d5ab452e'},
{'$set': {'result.$.isFavorite': True}})
pprint.pprint(list(db.mycollection.find())[0])
result:
{'_id': 'f08b466a-163b-4d9e-98f5-d900ef0f1a26',
'firstName': 'foo',
'result': [{'_id': '957ee97d-d461-4d6c-8a80-57351bdc29f7',
'marks': 60,
'subjectName': 'Mathematics'},
{'_id': '0591d9a0-fd0f-4876-9bd3-dec4d5ab452e',
'isFavorite': True,
'marks': 70,
'subjectName': 'Science'},
{'_id': '21f42104-791b-4522-81ce-f7ae1b30d075',
'marks': 50,
'subjectName': 'Social science'}]}

Resources