Question about NDB In query operator

 

I'm running a query that finds products that match a list of identifiers with the property name "asin":

asinList = ["id1", "id2", "id3",  "id4", "id5", ... ]

qry = DS_Product.query(DS_Product.asin.IN(asinList))

existingProducts = qry.fetch()

Lets say this returns 3 items.. Ideally I want to know which of the asin's weren't found because I want to create new DS_Product items for those.  Only way I can figure out how to do that is to loop over the returned products and remove the asin's from the orig list.. the ones left over were the ones that didn't return an existing product.  I'm trying to avoid that loop because that asinList can be quite long and I'm doing this in a few places.  Ideally there's a way to tell it to return the items in the list it didn't find or the list is always the same len as the orig, but has None for the index's that weren't found.

Any stratagies for this?

Thanks

5 1 100
1 REPLY 1

If I understand you correctly, you'd like to find all "ASIN"s which aren't linked to a Product. In that case, one way to do this (don't know if it's better than your current approach) is to have a separate model for ASINs e.g

class ASIN(ndb.Model):
    asin = ndb.StringProperty()
    no_of_products = ndb.IntegerProperty(default=0)

There'll be a row for each ASIN. Each time you create a product, you'll increment the count (no_of_products) of the corresponding ASIN. If you modify (or delete a product), you do the reverse. The call to update the ASIN model and the call to create/update a product will be done in a transaction.

To find ASINs without a matching product, you simply query the ASIN model (table) for those with a count of 0 i.e.

ASIN.query(ASIN.no_of_products == 0).fetch()

You can store your query result in memcache and only expire it (pop it) whenever you update the ASIN table. With the results in memcache, you don't have to rerun the query each time you wish to get the ASINs not linked to any product (you only query the datastore if the result isn't in memcache).

 

......NoCommandLine ......
https://nocommandline.com
        Analytics & GUI for 
App Engine & Datastore Emulator