Get user's personal data with aggregation

Hello, friends!

Let’s say you are authenticated in your Moralis dapp and run

const currentUser = Moralis.User.current();
currentUser.set("username", "my_new_username");

This will successfully add username to certain user in _User collection. But data will be encrypted, even if isEncryptedUserEnabled === false. You can only access username with Moralis.User.

Some data is not that sensitive and I need to use it in other collections. So, the question is, how to get all users username fields with aggregation from cloud functions? Now it returns something like “ExkNWei2KNA9f1oFgmC6QDl42” per user.

You have to use master key in cloud code to be able to get data for all the users.

As I said, I want get these info using aggregation.

pipeline = [
        match: {
            ethAddress: {$eq: "ANY_USER_ADDRESS"},

    const query = new Parse.Query("_User");
    const result = await query.aggregate(pipeline);

Where should I use useMasterKey here?

You can find an example here:

Already tried that.

const result = await query.aggregate(pipeline, {useMasterKey: true});

Returns “ExkNWei2KNA9f1oFgmC6QDl42” in username. I want it to return “my_new_username”

Do you see that new username in the database in the dashboard? That data is not encrypted.

No, in dashboard I also see something like “ExkNWei2KNA9f1oFgmC6QDl42”

Just re-run code and now it’s not encrypted -_-

100000% sure it was as I explained. Now suddenly it’s “my_new_username” in dashboard. Will write back if I will face same issue again.

That means that the update probably didn’t work. We added some protections so that the users can not change some fields directly, for example the eth address.

1 Like