I am using ndb model as my databse. what i am trying to do is that filter the result on the basis of list of ids.
i have my model like :
class Photo(ndb.Model):
userid = ndb.StringProperty()
source = ndb.StringProperty()
handle = ndb.StringProperty()
sourceid =ndb.StringProperty()
So i am trying query like this:
queryset=Photo.query(Photo.key.id().IN(photoid_list))
I have also tried :
queryset=Photo.query(Photo.id().IN(photoid_list))
where photoid_list is the list of ids.
Help me out to solve it.
I would suggest that you create keys from each id and then get them all at once:
photo_keys = [ndb.Key(Photo, id) for id in photoid_list]
photos = ndb.get_multi(photo_keys)
The advantage is that a get is faster than a query. Also, ndb will memcache the entities by key and make subsequent gets even faster.
There are examples in the docs:
https://cloud.google.com/appengine/docs/python/ndb/queries#neq_and_in
So it looks like your query should be:
queryset=Photo.query(Photo.id.IN(photoid_list))
Related
I have the following objects on cosmosdb
And my code with cosmonaut library its like this:
var pageTemplate = await pageTemplateStore.Query().FirstOrDefaultAsync(x => x.Id == id);
where id its either 0 or 1, but I am always getting null as a result
The problem is that you haven't decorated your object's Id field with [JsonProperty("id")] so the built in LINQ to SQL translator can't convert it. It can be found in Cosmonaut's documentation
However you should not query for documents using their id. This will perform a query instead of a read an it will cost way more RUs and more time. Use the FindAsync method instead which will do a direct read and will only cost 1RU.
I have a model named EmailDetailModel with a Dictionary field.
My query is:
var query = from email in context.EmailModel
where rolesForUser.Contains(email.GroupName)
orderby email.Id descending
select new EmailDetailModel()
{
From = email.From,
Id = email.Id,
message = email.Body,
subject = email.Subject,
pathFileName =
(from pdf in context.PdfModel
where pdf.Email_Id == email.Id
select pdf)
.ToDictionary(k => k.PathFile, v => v.FileName),
timeSend = email.ActiveTime};
However an error occurs on this line when trying to send the list to the view:
View(query.ToPagedList(pageNumber, pageSize));
Saying that:
LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary
If anybody could help me, I'm not sure how I can pass the key and value into my pathFileName Dictionary.
The issue likely is that there is no SQL like conversion of the dictionary generation that LINQ to Entities can understand.
I have struggled with similar error messages whenever LINQ cannot be translated to a SQL statement.
Cassandra support updating specific value in Collection by syntax
ALTER TABLE users ADD todo map<timestamp, text>
UPDATE users SET todo['2012-10-2 12:00'] = 'throw my precious into mount doom'
WHERE user_id = 'frodo';
http://www.datastax.com/documentation/cql/3.0/cql/cql_using/use_map_t.html
Did not see any example of using QueryBuilder to update specific row in Map. How it can be done?
I think you have several options.
1/ Build your own query based on the CQL one.
Example: Consider that you have a table named Interactions and in your schema a column of type named 'attributes'.
String update ="UPDATE demo.Interactions SET attributes=attributes + {'i':'j'} where id='ff';
SimpleStatement statement = new SimpleStatement(update);
session.execute(statement);
2/ Use Java API.
Java API is not that documented indeed.
Let's take an example
a- Create an update object using queryBuilder
Update update = QueryBuilder.update(keyspace, tableName);
b- Then populate with 'put' or 'putAll' functions. put/putAll will add/update content
update.with(QueryBuilder.putAll(key, map));
To remove a key, set the content of a key to null, like:
for (Object item : map.keySet()) {
update.with(
QueryBuilder.put(columName, item, null)
);
}
Then execute the query.
Following methods are available for different types:
LIST:
QueryBuilder.appendAll(...)
QueryBuilder.discardAll(...)
SET:
QueryBuilder.addAll(...)
QueryBuilder.removeAll(...)
MAP:
QueryBuilder.putAll(...)
QueryBuilder.put(...)
The list is not exhautive.
Have a look in QueryBuilder class if you do not find what you are looking for.
Best regards and best luck with Cassandra.
I need help in ydn db.
My issue is as follows: How can I fetch records by 2 conditions?
In sql it would be:
"SELECT * FROM store WHERE name='Test' AND cool=1;"
Currently my attempt looks like this:
var keyRange = ydn.db.KeyRange.bound(['Test', true]);
var db = DB.getDatabase();
database
.get('personStore', 'name, cool', keyRange)
.done(function(records) {
console.log(records);
});
Thanks in advance :-)
As discussed in this page, you can use compound index (as you did) or key joining. If you index ['name', 'cool'], you should be able to query by ydn.db.KeyRange.only(['Test', 1]). Note that boolean is not a valid key. Even if cool is boolean value, you have to index as integer value. But my suggestion is index only name and, just query name and filter out cool value on the result set.
All,
I have an entity, that has several collections,- each collection is mapped lazily. When I run a criteria query, I get duplicate results for my root entity in the result set. How's that possible when all my collections are mapped lazily!
I verified, my collections, load lazily.
Here's my mapping:
Root entity 'Project':
[Bag(0, Lazy = CollectionLazy.True, Inverse = true, Cascade = "all-delete-orphan")]
[Key(1, Column = "job_id")]
[OneToMany(2, ClassType = typeof(ProjectPlan))]
public virtual IList<ProjectPlan> PlanList
{
get { return _planList; }
set { _planList = value; }
}
The criteria query is:
ICriteria criteria = session.Session.CreateCriteria<Entities.Project>()
.Add(Restrictions.Eq(Entities.Project.PROP_STATUS, !Entities.Project.STATUS_DELETED_FLAG));
.CreateAlias(Entities.Project.PROP_PLANLIST, "p")
.Add(Restrictions.Eq("p.County", 'MIDDLSEX'))
.setFirstResult(start).setMaxResults(pageSize)
.List<Entities.Project>();
I know, I can correct this problem w/ Distinct result transformer, I just want to know if this is normal behavior on lazy collections.
EDIT: I found the cause of this,- when looking at the raw SQL, the join, and where clause are correct but what baffles me is the generated Select clause,- it not only contains columns from the project entity (root entity) but also columns from the project plans entity which causes the issue I described above. I am not at work right now, but I'll try to do this: .SetProjection(Projections.RootEntity()), so I only get Project's columns in the select clause.
One way, how to solve this (I'd say so usual scenario) is: 1) not use fetching collections inside of the query and 2) use batch fetching, as a part of the mapping
So, we will always be querying the root entity. That will give us a flat result set, which can be correctly used for paging.
To get the collection data for each recieved row, and to avoid 1 + N issue (goign for collection of each record) we will use 19.1.5. Using batch fetching
The mapping would be like this
[Bag(0, Lazy = CollectionLazy.True
, Inverse = true
, Cascade = "all-delete-orphan"
, BatchSize = 25)] // Or something similar to batch-size="25"
[Key(1, Column = "job_id")]
[OneToMany(2, ClassType = typeof(ProjectPlan))]
public virtual IList<ProjectPlan> PlanList
{
...
Some other similar QA (with the almost same details)
How to Eager Load Associations without duplication in NHibernate?
NHibernate QueryOver with Fetch resulting multiple sql queries and db hits
Is this the right way to eager load child collections in NHibernate
And we still can filter over the collection items! but we have to use subqueries, an example Query on HasMany reference