DynamoDB : query global secondary index on non-key attributes - php

I want to query global secondary index by applying a condition on non-key attributes of GSI. I have tried below code and its not working.
Code :
$result = $this->dbClient->query(array(
'TableName' => 'myTable',
'IndexName' => 'myIndex',
'AttributesToGet' => array('id'),
'KeyConditions' => array(
// Key attribute
'userId' => array(
'ComparisonOperator' => ComparisonOperator::EQ,
'AttributeValueList' => array(
array(Type::NUMBER => $value)
)
),
// This is non-key attribute
'length' => array(
'ComparisonOperator' => ComparisonOperator::LE,
'AttributeValueList' => array(
array(Type::NUMBER => $upperLimit),
)
),
),
));
EDIT :
I get error message
Query key condition not supported

You can only query on key attributes. You need to create a GSI on "length" - The reason GSI exist is for this purpose.

From http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
For a query on a table, you can only have conditions on the table
primary key
attributes. You must specify the hash key attribute name and value as an EQ
condition. You can optionally specify a second condition, referring to the range
key attribute.
For a query on an index, you can only have conditions on the index key attributes.
You must specify the index hash attribute name and value as an EQ condition. You
can optionally specify a second condition, referring to the index key range
attribute.

Related

MongoDb + PHP: Cannot specify more than one positional proj. per query

I have a "users" collection containing 2 arrays of objects (cards and prizes) each of these 2 can contain data from different companies marked with the "id_company" field.
$list = $users->find(
array(
'cards.id_company' => '... id company ...',
'cards.is_active' => true,
),
array(
'projection' => array(
'_id' => 1,
'full_name' => 1,
'cards.$.id_company' => '...id company...',
'prizes.$.id_company' => '...id company...',
),
'limit' => 5,
)
);
I'm trying to select only the data relating to one company, consequently excluding the others, but I get the following error: Cannot specify more than one positional proj. per query.
Any solutions?

PHP - MongoDB Aggregation: GROUP BY on multi-type field

I have a field in my MongoDB Collection that is hosting two types of data. In some Documents that field has Integer value, e.g.
"campaign_code" : NumberLong(100097)
And in other Documents that field has Array value, e.g.
"campaign_code" : [NumberLong(100087), NumberLong(100136), NumberLong(100137), NumberLong(100138), NumberLong(100135)]
Now, previously I was grouping my result by "campaign_code", but at that time it had only Integer values. Now, the field is having two types of values. The question is is PHP MongoDB driver intelligent to perform the same functionality or do I need to change my code?
My previous PHP code:
$pipeline = array(
array('$match' => array('impression.affiliate_id' => $affiliate_id)),
array(
'$group' => array(
'_id' => array(
'impression.campaign_code' => '$impression.campaign_code'
),
'count' => array('$sum' => 1)
)
),
//sort
array('$sort' => array('count' => -1))
);
I did make some changes and added the following line of code:
array('$unwind' => '$impression.campaign_code')
But this throws an exception:
exception: Value at end of $unwind field path '$impression.campaign_code' must be an Array, but is a NumberLong64
Now the exception is quite valid because few documents have only Integer value in the field. Tell me how I can resolve this issue?

CakePHP custom values on select input

I am trying to create a select input field. However I want to set the values of each individual option manually.
in an attempt I tried the following:
echo $this->Form->input('field', array(
'options' => array('Active', 'Blocked', 'Pending', 'Unknown'),
'values' => array(1,2,0,99),
'empty' => '(choose one)'
));
However this did not help (i.e 'Active' was 0, 'Blocked' was 1 etc...)
Does anyone know if it is possible to manually set the values?
values is not the right key, you need to leverage the options array for it, as well:
'options' => array(1 => 'Active', 2 => 'Blocked', 0 => 'Pending', 99 => 'Unknown'),
but that is basic PHP (since non-defined keys are numerically indexed starting off at 0).
You’ll need to use an associative array to set the keys as well:
$options = array(
'1' => 'Active',
'2' => 'Blocked',
'0' => 'Pending',
'99' => 'Unknown'
);
echo $this->Form->input('field', array('options' => $options));
However, I’d advise storing options like this in a separate database table rather than hard-coding them, to keep your views DRY and allowing them to be easily modified in the future.

$addToSet and $inc for updating a row

I'm working on a rating system. When a user rates, $inc increments the field, and addToSet adds the user_id to make sure the user only clicks rate once. I am checking if the user_id is already in the x field before updating, but that is another query which I'd rather avoid. Can I reach this purpose without having to write another query? I mean, $addToSet only adds if there is no value like that; can I instead get affected rows? Can you suggest other queries?
Thank you!
..->update(
array("_id" => $idob),
array(
'$inc' => array($type => (int) 1),
'$addToSet' => array("x" => (int) $user_id)
)
);
Ok I see the problem.
..->update(
array("_id" => $idob),
array(
'$inc' => array($type => (int) 1),
'$addToSet' => array("x" => (int) $user_id)
)
);
The problem is that you need a conditional $inc there so that it only $incs if it does add to set.
This is not possible with a unique index since unique indexes work from the root of the document atm. Also you probably want to use the $inc as a form of pre-aggregation or what not.
One method could be:
update(
array('_id' => $idob, 'x' => array('$nin' => array($user_id))),
array(
'$inc' => array($type => 1),
'$push' => array('x' => (int)$user_id)
)
)
This will only do the update if that user_id does not already exist in x.

how to use insert_batch with unique ids

I am using codeignitor's insert batch function to insert multiple rows to a table.
$this->db->insert_batch('table', $sizes);
my $sizes array looks like this
$sizes = array(
array(
'size' => 'M' ,
'product' => 'Hat'
),
array(
'size' => 'L' ,
'product' => 'Hat'
)
);
I was intending for separate rows to be added to my db table with these values along with unique ids, however when each nested array is added, It adds 0 to the id field, rather than a unique ID
I am receiving the error "Duplicate entry '0' for key 'id'"
What is the best approach to solve this? Thanks for reading!
Make your UNIQUE Column with AUTO_INCREMENT

Categories