1

I was reading on how to query in moralis because I have to use something like that however, the docs does not seem to explain what I intend to do and I have been there for close to 2hrs and I seem to catch nothing to use

so my query is suppose to be close to the domo https://v1docs.moralis.io/moralis-dapp/database/queries#queries-on-array-values except in their case the array contained only number in my case it is an array of many objects like this

{
"name":"some name",
"attributes": [
    {
        "trait_type": "Background",
        "value": "Aquamarine",
        "rarityScore": 7.912183544303797
    },
    {
        "trait_type": "Fur",
        "value": "Gray",
        "rarityScore": 20.163306451612904
    },
    {
        "trait_type": "Clothes",
        "value": "Pimp Coat",
        "rarityScore": 125.0125
    },
    {
        "trait_type": "Mouth",
        "value": "Bored Unshaven Dagger",
        "rarityScore": 357.1785714285714
    },
    {
        "trait_type": "Eyes",
        "value": "Closed",
        "rarityScore": 14.105782792665725
    },
    {
        "trait_type": "Hat",
        "value": "Bayc Hat Black",
        "rarityScore": 43.864035087719294
    },
    {
        "trait_type": "TraitCount",
        "value": 6,
        "rarityScore": 15.04192517390487
    },
    {
        "trait_type": "Earring",
        "value": null,
        "rarityScore": 1.4236298932384341
    }
],
}

so what am doing is, my query should filter the objects on attributes where i supply trait_type and value

I tried

query.equalTo("attributes", filterQuery);

but it does not work coz in my case filterQuery is an array with different objects each with unique trait_type and value

and example of filterQuery am using is

 [
    {
        "trait_type": "Fur",
        "value": "Blue"
    }
]

will appreciate very much if someone can help me solve this or even an idea on how it is done

kim
  • 159
  • 12

1 Answers1

1

Hi if attributes is a column of array with an array of objects value:

[
    {
        "trait_type": "Background",
        "value": "Aquamarine",
        "rarityScore": 7.912183544303797
    },
    {
        "trait_type": "Fur",
        "value": "Gray",
        "rarityScore": 20.163306451612904
    },
    {
        "trait_type": "Clothes",
        "value": "Pimp Coat",
        "rarityScore": 125.0125
    },
    {
        "trait_type": "Mouth",
        "value": "Bored Unshaven Dagger",
        "rarityScore": 357.1785714285714
    },
    {
        "trait_type": "Eyes",
        "value": "Closed",
        "rarityScore": 14.105782792665725
    },
    {
        "trait_type": "Hat",
        "value": "Bayc Hat Black",
        "rarityScore": 43.864035087719294
    },
    {
        "trait_type": "TraitCount",
        "value": 6,
        "rarityScore": 15.04192517390487
    },
    {
        "trait_type": "Earring",
        "value": null,
        "rarityScore": 1.4236298932384341
    }
]

Based on your filterQuery example, you could do something like:

query.equalTo("attributes.trait_type", "Fur");
query.equalTo("attributes.value", "Blue"); 

Which will only return an Object that has that particular trait_type and value combination in its attributes.

If the value of attributes is like the example you posted that has "name":"some name", you would use (again for a column name of attributes):

query.equalTo("attributes.attributes.trait_type", "Fur");
query.equalTo("attributes.attributes.value", "Blue"); 

If you have more questions or issues with this, please post on either:

Alex
  • 66
  • 1
  • 3
  • Hello @Alex this works for one trait for instance in the one above "Fur" what if am trying to filter from more than one trait? – kim Aug 30 '22 at 06:47
  • You can just add another set of `query.equalTo` constraints for that additional trait_type/value. – Alex Aug 30 '22 at 20:42