GQL Projection Query on Embedded Entity - google-cloud-datastore

Say I have a User entity with an embedded entity address which contains a house number property and a street property. How can I write a GQL projection query that will filter based on name and return a list of addresses ?
Sample Data:
The projection query I am trying to write:
SELECT address FROM User WHERE name = Bob
This should return two addresses but it returns no results.
Thanks to anyone who answers !

You can't project the entire entity value, but you can project the individual properties in the entity value, e.g.:
SELECT address.houseNumber FROM User WHERE name = Bob

Related

How to Query Realm DB Browser?

I need to filter the DB on the basis of Id. On top there is search bar stating "Enter the query to filter the list". How to query to get some specific id filtered in RealmDB browser.
My DB URL: https://i.stack.imgur.com/G1iXY.png
That field is used to enter queries aka Filter. Suppose you have a Dog Class with a dog_name property. To search for a Dog with the name Fido, you would enter this into the field
dog_name == "Fido"
The field is evaluated live so as soon as a valid query is entered, the results are updated.
If you look at the docs (this is Swift) you will see an example like this
// Query using a predicate string
var tanDogs = realm.objects(Dog.self).filter("color = 'tan' AND name BEGINSWITH 'B'")
To apply the same filter, just use the part within the quotes in Realm Studio
color = 'tan' AND name BEGINSWITH 'B'

Firestore: How to query data from a map of an array

Here is the schema for test db:
I want to write a query that can get all the documents where contacts have id=1 in any of the array index.
I have checked array_contains operator for firestore but the thing is my array have map which then have field id.
Thanks
You currently can't build a query that looks for an object field within an array. The only things you can find in an array are the entire contents of an array element, not a part of an element.
With NoSQL type databases, the usual practice is to structure you data in a way that suits your queries. So, in your case, you're going to have to structure your data to let you find documents where an array item contains a certain string. So, you could create another array that contains just the strings you want to query for. You will have make sure to keep these arrays up to date.
You could also reconsider the use of an array here. Arrays are good for positional data, but unless these contacts need to be stored in a certain order, you might want to instead store an object whose keys are the id, and whose field data also contains the id and name.
As Doug said, you can't query it,
but, if you could structure your data to something that looks like this
Store your data as a map
Use id as key and name as value
Now you can write a query that can get all the documents where contacts have id=1
db.collection("test").where("contacts.1", ">=", "").get()
If you have an array of maps or objects and want to get the docoments that contains this specific map/object like this ?
array_of_maps.png
you can use
queryForCategory(categName: string, categAlias: string): Observable[] {
firestore.collection('<Collection name>', ref =>
ref.where('categories',
"array-contains", {
"alias": categAlias,
"title": categName
}
One soulions is as #Doug said, to add array with the data you need to query, and keep it uptodate.
Another solution is to make contacts as sub collection of the document, an then you could make queries of the contacts and get its parrent.
final Query contacts = db.collectionGroup("contacts").whereEqualTo("id", 1);
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
System.out.println(document.getId());
System.out.println(document.getString("parent_id"));
}
If you don't want to add parent as another field, you can get it from parent reference, check this answer https://stackoverflow.com/a/56223319/1278463
snapshot.getRef().getParent().getParent().collection("people")
https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query

Is it possible to query an element in Firestore by key without knowing which collection it belongs to?

Using AngularFire2, Angular, Firebase Firestore, and one of my records models the relationship between a user and different types of objects.
/*
Represents a reaction that a user has towards some object in the system.
*/
export interface Reaction{
// The id of the user making that reaction
userId? : string;
// The id of the object that is being reacted to. Place, Org, List, Offer
objectId? : string;
}
As you can see the only thing being stored is the key of an object and not its type or which collection it belongs to. I'm wondering how it would be possible at a later time, to query the reactions and then from there get the objects purely based on their key?
You must know the name of the collection (and possibly subcollection) of a document in order to obtain it. There's no concept of a query that can get a document without knowledge of a collection.

DynamoDb map vs string attribute read /update

I have a dynamodb table where partition key is userid uniformly distributed. Table store data about User information like(name , email,country, and portal cards the user hold- say user1 has cards1,cards2,cards3 (max 10 category of cards) with detail of cards1 as (gold card,purchased date-yyyy-mm-dd).A user can have Queries on table will be by userid.
I need to get how many cards with name and type user1 hold,
need to get country of user1
Need to get does user1 hold any card of card1 category of gold type
hold ,which were purchased in current year and whats the country of
user1.
To achieve this I can design my schema in 2 ways:-
Each item has attributes(other than userid partition key) as (Country-string,name string,email string,cards
map{"card1":{"purchasedate":"yyyy-mm-dd","type":"gold"},"card2":{{"purchasedate":"yyyy-mm-dd","type":"platinum"}}}
Make userid as primary key and card category as sort key and keep user info in item with key as userid_all and card info in item with
key as userid_card1 and attributes of string type as purchasedate
and type. say userid1_all : country:Us,email:abc#abc.com
userid_card1: purchasedate:yyyy-mm-dd,type:gold
Which approach is better ?
Reading data from map attribute is better or from string attribute.
Updating attributes with map type(say userid1 for card1 type is upgraded from gold to platinum) is better or string type.
Thanks
Though DynamoDB supports Document data types (Map, List etc), the query API is not very robust. In your design approach 1, if you convert the data into DynamoDB item, you will actually end up with complex object (i.e. card1 value is an object with two attributes purchasedate and type) for cards attribute. The third query pattern has the requirement to filter by purchasedate and type which DynamoDB API doesn't support as it is inside another object of MAP data type rather than some scalar attribute inside MAP.
Simple object of MAP:-
{"purchasedate":"yyyy-mm-dd","type":"gold"}
Complex object of MAP:-
{"card1":{"purchasedate":"yyyy-mm-dd","type":"gold"},"card2":{"purchasedate":"yyyy-mm-dd","type":"platinum"}}
So, the design approach 2 is better. As long as you have scalar attributes (i.e. String, Number, Boolean), you can query the table by different combinations of attributes in FilterExpression.

can you create indexes on embedded objects in dynamodb

Coming from a mongodb background, I'd like to set up a document with an embedded collection.
For instance if I have a profile object
Profile
name : string
followers : [
name: string
]
such that it has an embedded collection of followers.
Is there a way that I can create an index on Profile so that I can query for all profiles where Profile.Followers includes myUsername?
In short I can query for profiles I'm following from a dynamoDB table?
In mongo I can easily do this by setting up an index on Profile.followers and doing an $in query. Is there something similar for dynamodb?
This documentation suggests there is nothing like an in clause
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html
Currently DynamoDB does not support indices for non scalar types (i.e. Set, List, or Map data types - see here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html). If you have a separate users table, you can keep track of all profiles you are following in a Set/List attribute.

Resources