Moralis tables secondary indexes

I need to add a secondary index to moralis table.
how can I do that ?

I have a column (urlPath) in the table called collections ,
this url path is a unique value for a item (collection) in a table.
I need to get a single collection by using urlPath as input.

I can use the query.equalTo(“urlPath”,“mycollectionone”)

Is this good when there are many collections ?

I hope it would be better if we hv a secondary index so I can get the item using some think like below
query.get(“mycollectionone”) ?

can u plz help me on this.

you can connect directly to mongo and create an index there

after creating the secondary index how to query it using moralis sdk?

I don’t know exactly what problem you have on query, can you give more details?

what did you try?

I am creating a sample nft market place.

here I hv a table for the collections called (FujiCollections).
I created the collections using.

const Collection = Moralis.Object.extend(“FujiCollections”);
const collectionItem = new Collection({name:‘name’,displayImage:‘displayImage’,symbol:‘symbol’,description:‘description’,urlPath:‘sample-url-path’,contractAddress:‘contractAddress’,account:’’});
collectionItem.save();

then I need to query the table FujiCollections from urlPath, for that I used,

const query = new Moralis.Query(Collection);
query.equalTo(“urlPath”, “sample-url-path”);
const collectionResult = await query.first();
console.log(collectionResult);

this is working without a issue.

my concern is that when there are many entries in the FujiCollections table, this type of query will get slow.To get the result fast I hope to use the urlPath as a secondary index. So I can query faster.

Screenshot 2021-12-22 at 01.17.21

what I try to achieve some think like above,When a user try to add a “short url” , in my case urlPath,
I need to check the db quickly and find out a similar entry exits in the db.

you can add an index for that that urlPath field after you connect directly to mongo db

1 Like

I added it. After adding it , do I need to use the same query.

const query = new Moralis.Query(Collection);
query.equalTo(“urlPath”, “sample-url-path”);
const collectionResult = await query.first();
console.log(collectionResult);

or can I use some get function in moralis sdk .to get the single item directly

I think that you can use the same query directly

1 Like

added 10000 dummy data , tested and now it is fast

thank you very much for the support