Home > other >  Filtering a nested array with scan in dynamodb
Filtering a nested array with scan in dynamodb

Time:01-25

I trying to work out the DynamoDB scan with the nested array. I trying to get the user details base on the survey id in the nested array, My table value is like this.

{
 "id": "28009dd8-5802-20f5-c7c9-af53e710ac91",
 "user": {
  "address1": "1, Park avenue",
  "address2": "Austin",
  "birthDate": "1999-01-01",
  "city": "Austin",
  "contact": "9876543210",
  "country": "1",
  "email": "[email protected]",
  "emgContact": "2",
  "empStatus": "1",
  "ethnicity": "5",
  "firstName": "Rajmohan",
  "gender": "male",
  "highLevelEduc": "2",
  "image": "",
  "lName": "V",
  "mName": "",
  "nationality": "1",
  "relationalStatus": "1",
  "state": "43",
  "telephone": "9876543210",
  "zipCode": "70031"
 },
 "survey": [
  {
   "id": "e8de014a-22c1-12bf-4653-577c8031138",
   "email": "[email protected]",
   "method": [
    "email"
   ],
   "name": "Rajmohan",
   "onDemand": "yes",
   "pollSchedule": "None",
   "pollTarget": "Educator",
   "telephone": "963696369",
   "user_id": "413ca05f-ed91-50e7-1974-3e0280ca4a3d"
  },
  {
   "id": "5f4db059-c8a3-e673-iygk-d857425e1077",
   "created_at": 1674459043374,
   "email": "[email protected]",
   "method": [
    "email"
   ],
   "name": "New testing",
   "onDemand": "yes",
   "pollSchedule": "None",
   "pollTarget": "Educator",
   "telephone": "963696369",
   "user_id": "413ca05f-ed91-50e7-2635-3e0280ca4a3d"
  }
 ],
 "summary": "test",
 "user": "413ca05f-ed91-8k8y-2635-3e0280ca4a3d"
}

and my scan syntax is like that.

var param = {
        TableName: 'user',
        FilterExpression: 'contains(#survey.#id,:id)',
        ExpressionAttributeNames: {
            '#survey': 'survey',
            '#id':'id'   
        },
        ExpressionAttributeValues: {
            ':id': id
        },
      }

But I can't get the value. My point is based on the survey id I need to get the value of the user.

CodePudding user response:

You cannot do what you're attempting, you would need to pass in the full value of the map which would be quite difficult to do i'm sure.

Using a list of maps

You need to know the entire map value when using contains function, as you are asking if the map is contained in the list, not if a string is contained in the map:

var mymap = {
   "id": "e8de014a-22c1-12bf-4653-577c8031138",
   "email": "[email protected]",
   "method": [
    "email"
   ],
   "name": "Rajmohan",
   "onDemand": "yes",
   "pollSchedule": "None",
   "pollTarget": "Educator",
   "telephone": "963696369",
   "user_id": "413ca05f-ed91-50e7-1974-3e0280ca4a3d"
  }
var param = {
        TableName: 'user',
        FilterExpression: 'contains(#survey,:mymap)',
        ExpressionAttributeNames: {
            '#survey': 'survey'
        },
        ExpressionAttributeValues: {
            ':mymap': mymap
        },
      }

Using nested maps

You should structure your list as a map:

 "survey": {
 "e8de014a-22c1-12bf-4653-577c8031138":{
   "email": "[email protected]",
   "method": [
    "email"
   ],
   "name": "Rajmohan",
   "onDemand": "yes",
   "pollSchedule": "None",
   "pollTarget": "Educator",
   "telephone": "963696369",
   "user_id": "413ca05f-ed91-50e7-1974-3e0280ca4a3d"
  },
  "5f4db059-c8a3-e673-iygk-d857425e1077": {
   "created_at": 1674459043374,
   "email": "[email protected]",
   "method": [
    "email"
   ],
   "name": "New testing",
   "onDemand": "yes",
   "pollSchedule": "None",
   "pollTarget": "Educator",
   "telephone": "963696369",
   "user_id": "413ca05f-ed91-50e7-2635-3e0280ca4a3d"
  }
 }
var id = "5f4db059-c8a3-e673-iygk-d857425e1077"
var param = {
        TableName: 'user',
        FilterExpression: 'attribute_exists(#survey.#id)',
        ExpressionAttributeNames: {
            '#survey': 'survey',
            '#id': id 
        }
      }

  • Related