How to search arrays for values in Google Cloud Datastore - google-app-engine

Assuming we have an entity in Google Cloud Datastore - "Job" and it has some Access Control Tags:
["admin", "manager"]
and we have a user that has some Access Control Tags
["admin", "super_user"]
We would like to pull back all Jobs that this user has access to; e.g. in SQL something like
SELECT * from Job where tags INCLUDE "admin" OR tags INCLUDE "super_user"
OR queries are not supported, is there another way to achieve this?

Could you query the datastore model using ndb.OR? Something like in this example:
query = Article.query(ndb.AND(Article.tags == 'python',
ndb.OR(Article.tags.IN(['ruby', 'jruby']),
ndb.AND(Article.tags == 'php',
Article.tags != 'perl'))))

You can query to retrieve multiple values like the following:
If you have array of multiple datastore Id's like following:
[1,2,3,4]
You can query with gstore-node Package
var userData = await UserModel.get([1,2,3,4]);
Hope this will help you.
Thanks

For that you need to store the data in array format in google cloud datastore. in here we can't use OR condition. so you can use > and < for equlity and also you can use the add_filter for that

Related

Modify and delete document in Cloudant IBM Database

Is it possible to modify document or delete in Cloudant Database by query?
I assume the questioner is looking for equivalent functionality to SQL's:
UPDATE db SET x = 10 WHERE y > 100;
If that is the question then the answer is that Cloudant does not have such functionality, only an atomic update operation.
The equivalent of the UPDATE statement could be achieved by combining a call to the Cloudant Query API and making updates using the bulk API.
Another option is to use the couchtato iterator tool which allows bulk changes to be made to Cloudant databases.
To delete a document you have to know the document _id, if you do not know the _id then you have to look for the document in your db, retrieve it and then get the _id.
To retrieve a document you can use a selector. For example, let's say that you have a document that contains a "name" field, your selector would be something like
selector = {"name": {"$eq": "name that you want"}}
So for python code you would have something like
def retrieve_db_data(db_name, selector):
client = connect_to_db()
db = client[db_name]
results = db.get_query_result(selector)
data = results.all()
return data
In "data" you will have the _id
then you can use something like this to delete the data
def delete_document(db_name, doc_id):
client = connect_to_db()
client.connect()
db = client[db_name]
document = db[doc_id]
document.delete()

Query by multiple doc_ids in Google App Engine Search API

I want to retrieve a list of documents from a list of doc_ids. Something akin to doing the following in SQL:
SELECT * FROM Documents WHERE id IN (1,2,3,167,91)
I see this method in the documentation, but that only retrieves a single document. For efficiency, I want to do a batch retrieval. Is this possible?
I don't think there's a batch get function, but there is an async version (search https://cloud.google.com/appengine/docs/python/refdocs/google.appengine.api.search.search for get_async), so you could do something like (untested pseudo-code):
# query for docs ids in advance
futures = []
for id_ in doc_ids:
futures.append(index.get_async(id_))
# dereference the futures when you need them
ids = [x.get_result() for x in futures]

Cloud Datastore 'like' query

I've an entity in Google Cloud Datastore. One of the properties is array of strings. For example:
property: skills
Entity 1:
value: ["mysql","sqlserver","postgresql","sqllite","sql-server-2008","sql"]
Entity 2:
value: ["css","css3"]
Now, I need to query for those entities that contain array elements css*
In typical SQL, it'll be select * from kindName where skills like 'css%'
I tried select * from kindName where skills = 'css', which works fine but how can I get entities that have css* elements similar to the SQL query?
Or
What's the best way to model the data for this?
You can do inequality range checks on a single indexed property as given in the example below. Range checks on strings are essentially how you can perform prefix searching on strings.
SELECT * from yourKind WHERE skills >= "css" AND skills < "cst"
As an example, here is the query performed on some sample data I created in the UI Console for Cloud Datastore:

Query by key in Datastore with Dart

I have a List<Key> which I would like to retrieve the full data records for but with applying additional filtering to it.
I can retrieve them via dbService.lookup(Project, keys) but lookup doesn't allow me to apply additional filtering.
This is essentially what I want to do:
dbService.query(Project)
..filter('__key__ IN', keys)
..filter('acl_read IN', roles)
..run();
but since __key__ is not supported in Google Cloud's Dart implementation, I cannot run this query.
I could do:
projects = dbService.lookup(keys);
projects.removeWhere((project) => (project.acl_read.fold(false, (result, key) => result || members.contains(key))));
but this seems not like the right way of achieving this.
So what's the right way of doing this?
There isn't a server-based method to do what you're looking to do, so your method of post filtering on the client-side is how you'd do it..
Alternatively, if you know that all querying all the keys with your filter results in a small set of keys then what you have in List, then do a full query first and then find the Union of results and List

Can I query by document id with full text search on App Engine?

After creating a document with a explicit id like this:
Document(doc_id = 123)
Is it possible to find that document using the id, similar to get_by_id with the Datastore?
I've tried queries with id:123, _id:123, doc_id:123, and _doc_id:123.
Index.list_documents has the options start_doc_id and limit which can be used to get a single document by id.
documents = index.list_documents(start_doc_id = id, limit = 1)
I also found in Java that you can do index.get(ID), which might be useful for Java users.
Here's the Javadoc:
https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/Index#get(java.lang.String)

Resources