I´m Looking for search values into my Datastore but I need information filtered by value. I know Datastore is no relational DB, but how could I get the goal?
I´m trying this:
$ds = new DatastoreClient(['keyFilePath' => 'foo.json');
$q = $ds->gqlQuery('SELECT * FROM EMPLOYEES WHERE NAME LIKE "%$'.val.'%"');
$res = $ds->runQuery($q);
The LIKE keyword is not supported, What I can use instead?
As Sammitch suggested, you can perform Datastore queries following the instructions described here or in this Stack Overflow post. The syntax is different, but the regular expression idea is the same.
Related
I've got a problem with the angularfire firestore query. Apparently the only working opStr in the where Query seems to be "==". I am trying to get documents with a value >= 1 but I am unable to get it to work.
So what I have tested is what I get when I query with different WhereFilterOps.
In my Firestore DB I've got a document with the key/value pair mytestvalue/1:number.
So I've created a query with the where clause:
.where("mytestvalue", ">=", 1)
And I do not get back the object. If I change the clause to:
.where("mytestvalue", "==", 1)
I do get the result I am expecting. Am I missing something? Do I need to add an index or something to be able to use the ">=" Operator?
I'm currently using #angular/fire 5.3.0. Do I need to upgrade to 6.0?
Thanks in advance!
Oh my god, sorry. I DID need an index. Guess I had the answer but was not able to use my brain properly.
I am using Python client SDK for Datastore (google-cloud-datastore) version 1.4.0. I am trying to run a key-only query fetch:
query = client.query(kind = 'SomeEntity')
query.keys_only()
Query filter has EQUAL condition on field1 and GREATER_THAN_OR_EQUAL condition on field2. Ordering is done based on field2
For fetch, I am specifying a limit:
query_iter = query.fetch(start_cursor=cursor, limit=100)
page = next(query_iter.pages)
keyList = [entity.key for entity in page]
nextCursor = query_iter.next_page_token
Though there are around 50 entities satisfying this query, each fetch returns around 10-15 results and a cursor. I can use the cursor to get all the results; but this results in additional call overhead
Is this behavior expected?
keys_only query is limited to 1000 entries in a single call. This operation counts as a single entity read.
For another limitations of Datastore, please refer detailed table in the documentation.
However, in the code, you did specify cursor as a starting point for a subsequent retrieval operation. Query can be limited, without cursor:
query = client.query()
query.keys_only()
tasks = list(query.fetch(limit=100))
For detailed instruction how to use limits and cursors, please refer documentation of the Google Gloud Datastore
I enjoy working with LiteDB in Xamarin.Forms. What is the best practice for getting list of objects from a table using list of ids or indexes in condition?
Currently, it looks for me like this:
_db.GetCollection<T>().FindAll().Where(q => listValues.Contains(Convert.ToInt32(q.GetProperty(idColumnName))));
listValues - list of searched ids.
idColumnName - column with indexes.
But FindAll retrives all records from LiteDB. Is there any more efficint options without full scanning?
Use the query in the Find method instead which will only retrieve entries based on your query rather than the full data set. This, according to the LiteDB's guidance, is more efficient than Linq queries on the full data returned: https://github.com/mbdavid/LiteDB/wiki/Queries
So your query would look like as follows
_d.GetCollection<T>().Find(q => listValues.Contains(Convert.ToInt32(q.GetProperty(idColumnName))))
I think you can just use collection.Query().Where("Column", 1,2,3) for this
I am trying to build a query via query builder.
$photosQuery = $photoRepository->createQueryBuilder('p')
->join('AppBundle:User', 'u')
->where('LOWER(p.title) LIKE :phrase OR LOWER(u.username) LIKE :phrase AND p.isActive = :isActive AND p.isModerated = :isModerated')
->setParameter('phrase', '%'.strtolower($phrase).'%')
->setParameter('isActive', true)
->setParameter('isModerated', true)
->getQuery();
It gives me
SELECT p0_.id AS id_0,
p0_.title AS title_1,
p0_.description AS description_2,
p0_.name AS name_3,
p0_.creation_date AS creation_date_4,
p0_.edit_date AS edit_date_5,
p0_.is_moderated AS is_moderated_6,
p0_.moderation_date AS moderation_date_7,
p0_.is_active AS is_active_8,
p0_.user_id AS user_id_9,
p0_.category_id AS category_id_10
FROM photos p0_
INNER JOIN users u1_ ON (LOWER(p0_.title) LIKE ?
OR LOWER(u1_.username) LIKE ?
AND p0_.is_active = ?
AND p0_.is_moderated = ?)
Why are my WHERE parameters in the join ON() portion and not a traditional WHERE?
Thank you!
Doctrine provides a wrapper around lower-level database connections that to not necessary have all the features present in DQL. As such, it emulates some features (such as named parameters, or splitting array parameters into multiple separate values).
You're seeing that in action here: the named parameters are converted into positional parameters in the raw query. Doctrine still knows what the mapping is, though, and is able to correctly order them when the query and parameters are sent to the server.
So the answer from jbafford explained some information Doctrine but did not explicitly answer why the ON was used instead of the WHERE, and it stems from a common mistake (of which I recently made as well).
In your query, when you use
->join('AppBundle:User', 'u')
you are simply telling the query builder that you need to join on the User table, but it doesn't specifically know that you want to join on the user association of your Photo entity. You may think - why doesn't this happen automatically? Well, imagine if you had 2 another association on your table that linked to a User entity as well (maybe a createdBy field or similar). In that case Doctrine wouldn't know the association you wanted.
So, instead the proper thing to do is join directly on your association rather than generically on the entity, like so:
->join('p.user', 'u')
and then Doctrine will handle the rest. I couldn't actually tell you why it uses the where() condition for the join, unless it's just assuming that's what you wanted, since it needs to know how to join on something.
So just remember that when you are joining on an association you already defined, join on the association as described in your entities rather than thinking of it in a straight SQL format where you'd join on the table.
I can't seem to find any good examples but what is the best way to do a query like this
tbl.Query(SELECT * FROM PRODUCTS WHERE ProductId IN (#Ids)", idlist)
I see that Dapper ORM, you can just put in a list and it knows what to do. Can you do this with Massive or do I have to build out the parameters like #0,#1,#2 and turn the list into an array idList.ToArray()?
It doesn't look like this is possible. When I tried (ids was an array in my case) I got the error:
No mapping exists from object type System.Int32[] to a known managed provider native type.
Which tends to make me think that Massive is largely letting the underlying SQL provider deal with the parameterization of the query. So yes, you are going to have to do build the parameters like you said. You could do something like this:
productTable.Query(string.Format("SELECT * FROM PRODUCTS WHERE ProductId IN ({0})",
string.Join(",", ids.Select((id, ix) => "#" + ix).ToArray())),
ids.Cast<object>().ToArray());