Query data from MP


#1

I was trying to query the data from MP using MPRester, but ran into a really weird problem. Below is the codes I used:

from pymatgen import MPRester, Composition

mpr = MPRester()
data = mpr.query(criteria = {‘elements’: [‘Ti’, ‘O’], ‘nelements’: 2},
properties=[“formation_energy_per_atom”, “energy”, “unit_cell_formula”,
‘nsites’, ‘spacegroup’])
len(data)

However, the length in data is 0, which is apparently wrong. But if I change ‘Ti’ to ‘Fe’, I can get the data. Any idea why it happens? What did I do wrong?


#2

Hi jlc198,

Generally queries trying to match array subfields are order-specific, and element arrays are ordered alphabetically, so the following works:

mpr = MPRester()
data = mpr.query(criteria={"elements": ["O", "Ti"], "nelements": 2}, 
                 properties=['formation_energy_per_atom', 'energy', 
                             'unit_cell_formula', 'nsites', 'spacegroup'])
len(data)
>>> 84

If you’d like to do a match on all of the fields of elements while constraining nelements, you can use the $all operator to do this in an order-agnostic way, e. g.

mpr = MPRester()
data = mpr.query(criteria={"elements": {"$all": ["Ti", "O"]}, 
                           "nelements": 2}, 
                 properties=['formation_energy_per_atom', 'energy', 
                             'unit_cell_formula', 'nsites', 'spacegroup'])
len(data)
>>> 84