How do I query datastore entities that have any value for a certain property efficiently?

StackOverflow https://stackoverflow.com/questions/22630950

سؤال

I have the following model

class User(ndb.Model):
    name = ndb.StringProperty()

I can query all the User entities that have no name by doing the following:

users_without_names = User.query(User.name == None).fetch(100)

But how do I query User entities with names, without using != None?

I also need to add another inequality filter on another property, and using != will translate into an equality filter, so I'd like to avoid !=.

هل كانت مفيدة؟

المحلول 2

The Note in != and IN Operations documentation warns that Entities without a property are not indexed. It is more efficient to choose a sentinel value such as "_no_name_" that will be indexed, and search for that. Of course your remaining code must process sentinel values as blank where appropriate.

نصائح أخرى

You could search for all names > ""

users_with_names = User.query(User.name > "").fetch(100)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top