COPINE Patrick
COPINE Patrick

Reputation: 1

No data with DynamoDB query

Unable to retrieve the record for the Nodejs query. The data (in JSON format) is given below along with the code. My data contains nested attributes which makes query queries difficult. Although the Nodejs query is valid, you can not have a data result.

My JSON data structure under DynamoDB is:

[
  {
    "department": 1,
    "city": "city01",
    "info": {
      "markets": [
        { 
          "name": "marché de city01",
          "day": "vendredi matin",
          "location": "place de la république",
          "frequence": "hebdomadaire",
          "category": "tous produits",
          "merchants": "40",
          "link": "https://www.city01.html"
        }
      ]
    }
  },
  {
    "department": 2,
    "city": "city02",
    "info": {
      "markets": [
        {
          "name": "marché de city02", 
          "day": "samedi matin",
          "location": "place de la liberté",
          "frequence": "hebdomadaire",
          "category": "tous produits", 
          "merchants": "80",
          "link": "https://www.city02.html"  
        },
        {
          "name": "marché de city02", 
          "day": "lundi après-midi",
          "location": "place de la mairie",
          "frequence": "bi-mensuel",
          "category": "tous produits", 
          "merchants": "60",
          "link": "https://www.city02.html"  
        }
      ]
    }
  }
]

My Nodejs code is:

var AWS = require("aws-sdk");

AWS.config.update({
  region: "eu-west-1"
});

var docClient = new AWS.DynamoDB.DocumentClient();

// recherche par contenu
console.log("Querying for markets of 1 (Ain) for city equal: city01");

var params = {
  TableName : "TestMarkets",
  ProjectionExpression:"#dep, #city, #info",
  KeyConditionExpression: "#dep = :dep and #city = :city",
  FilterExpression: "contains (#market, :ddd)",
  ExpressionAttributeNames:{
    "#dep": "department",
    "#info": "info",
    "#city": "city",
    "#market": "info.markets"
  },
  ExpressionAttributeValues:{
    ":dep": 1,
    ":ddd": {
      "day": "vendredi matin"
    },
    ":city": "city01",
  }
};

docClient.query(params, function(err, data) {
  if (err) {
    console.log("Unable to query. Error:", JSON.stringify(err, null, 2));
  } else {
    console.log("Query succeeded.");
    console.log(JSON.stringify(data)); 
    data.Items.forEach(function(item) {
      console.log("Dep: ", item.department + ", City: " + item.city);
      console.log(JSON.stringify(item.info));
    });
  }
});

Why The result gives no items? Thank you.

Upvotes: 0

Views: 721

Answers (1)

andrhamm
andrhamm

Reputation: 4411

For starters, the value of ExpressionAttributeValues in your query is invalid.

ExpressionAttributeValues: {
  ":dep": {
    N: "1"
  },
  ":ddd": {
    M: {
     "day": {
       S: "vendredi matin"
     }
    }
  },
  ":city": {
    S: "city01"
  }
}

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-ExpressionAttributeValues

Upvotes: 1

Related Questions