Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help wanted! Update in nested data #293

Closed
MockingMagician opened this issue Oct 5, 2022 · 1 comment
Closed

Help wanted! Update in nested data #293

MockingMagician opened this issue Oct 5, 2022 · 1 comment

Comments

@MockingMagician
Copy link

Hello to everyone,

I need help, let's say we have a bin with this structure:

{
  loc' : {
    EU' : {'FR' : {'Paris', 'Lyon'}}, 
    US': { 'CA': { 'Miami'}}
}, 

Is it possible to update loc.EU and add for example ES: {'Madrid'}}

So we would finally get this bin structure:

{
  loc' : {
    EU' : {'FR' : {'Paris', 'Lyon'}, ES : {'Madrid'}}, 
    US' : {'CA' : {'Miami'}}
}, 

If it is possible to do this, can someone provide a complete example?

Thanks in advance

@juliannguyen4
Copy link
Collaborator

Hi @MockingMagician, you would use client.operate() to insert nested map key-value pairs. To do this, you would create an operation that puts a new map item, and you would use a list of contexts to specify where to insert this item in the bin.

Full example:

import aerospike
config = {"hosts": [("127.0.0.1", 3000)]}
client = aerospike.client(config)

key = ("test", "demo", 1)
binName = "binName"
binValue = {
    'loc': {
        'EU': {'FR': ['Paris', 'Lyon']}, 
        'US': {'CA': ['Miami']}
    }
}
binPair = {binName: binValue}
client.put(key, binPair)

# Perform operation
from aerospike_helpers import cdt_ctx
# Iterate through the bin document: loc -> EU
ctxs = [
    cdt_ctx.cdt_ctx_map_key("loc"),
    cdt_ctx.cdt_ctx_map_key("EU"),
]
# Insert ES: Madrid
import aerospike_helpers.operations.map_operations as map_ops
ops = [
    map_ops.map_put(binName, "ES", "Madrid", None, ctxs)
]
client.operate(key, ops)

# Verify results
_, _, bins = client.get(key)
import pprint
pprint.pprint(bins)

# Cleanup
client.remove(key)
client.close()

This is the output of this script (formatted for clarity):

{'binName': {
    'loc': {
        'EU': {
            'ES': 'Madrid',
            'FR': ['Paris', 'Lyon']
        },
        'US': {
            'CA': ['Miami']
        }
    }
}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants