Reputation: 1
I have a 2 kinds of entity;
In the above kinds of entity, a Child is associated with a parent key.
I need to filter for a child based only on a child key as I dont have the parent key, only the child key/id.
const query = datastore.createQuery('Child')
.filter('__key__', '=', datastore.key(['Child', 123]))
The strange thing is, if I pass a ">" operator it will return records, however I need an exact key match, as well as an explanation.
If I have to add a composite index, how would I do so for the above relation?
Upvotes: 0
Views: 737
Reputation: 2612
as you can see here you can doi it like this, by adding the limit(1) you make sure of not getting multiple results.
const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore({
projectId: 'cf-datastore',
keyFilename: 'datastore-credential.json'
});
const kindName = 'user-log';
async getlog = (req, res) => {
let uid = req.query.uid || req.body.uid || 0;
//let log = req.query.log || req.body.log || '';
const key = datastore.key(['uid','log']);
datastore
.get(key,(err, entity) => {});
const query = datastore.createQuery('uid');
query.filter('uid', uid);
query.limit(1);
const transaction = datastore.transaction();
console.log("Transaction");
console.log(transaction);
const results = datastore.runQuery(query, (err, entities) => {
// entities = An array of records.
// Access the Key object for an entity.
firstEntityKey = entities;
console.log("Entities")
console.log(entities);
});
console.log("GOT IT");
console.log("Results")
console.log(results);
//console.log("Entity");
//console.log(entity);
const task = await datastore.runQuery(query);
console.log(task);
//second part
res.status(200).send(results);
};
exports.executed = (req, res){
console.log(` CALLING`);
getlog(req, res)
setTimeout(() => {
resolve('resolved');
}, 20000);
}
Upvotes: 0
Reputation: 1148
How are you assigning the child entity key ids? If it is using the allocate id api, then that API doesn't guarantee uniqueness across child entities belonging to different parent entities. As a result, even with a separate index, you can't uniquely query for the child.
One approach is to allocate ids for a top level entity and then use that id as a property on your child entity. Then you can query using that property and get a unique child associated with it.
Upvotes: 0