Custom Table related with Users table, returns all Users fields

Hi,

I have a table called “notification_groups”, this table is related to Users table in 1:N relation (the relation field is called users_related)

I want to obtain only the objectId from users related, also, I’m getting the count of users related:

let dataQuery = Backendless.DataQueryBuilder.create();
dataQuery.setProperties("group_name, count(users_related) as users_count, users_related.objectId as userId");
dataQuery.setRelated("users_related")
dataQuery.setGroupBy("group_name")
Backendless.Data.of('notification_groups').find(dataQuery)

I want to get a response like this:

[
  {
    "___class": "notification_groups",
    "group_name": "Group1",
    "countUsers": 3,
    "objectId": "41FAECFE-6270-486E-B342-85D031E0A1F2",
    "users_related": [
      {
        "objectId": "4662FB55-37D4-44E8-9713-9CC5004FEA5E"
      },
      {
        "objectId": "7FD36617-B643-98F4-FF2E-AF86E2CBD900"
      },
      {
        "objectId": "EE62C374-B550-BB3C-FF9B-9D2EC0058A00"
      }
    ]
  },
  {
    "___class": "notification_groups",
    "group_name": "Guests",
    "countUsers": 0,
    "objectId": "542793EA-A18C-426D-B9BF-96BC0F6ADA8A",
    "users_related": []
  }
]

But instead of that, I got all fields for each user:

[
	{
		"___class": "notification_groups",
		"group_name": "asdadasd",
		"UserId": null,
		"countUsers": 3,
		"objectId": "41FAECFE-6270-486E-B342-85D031E0A1F2",
		"users_related": [
			{
				"lastLogin": 1602026861000,
				"userStatus": "GUEST",
				"created": 1602026861000,
				"socialAccount": "BACKENDLESS",
				"___class": "Users",
				"updated": 1602026871000,
				"email": "XXXXXXXXXXXXXXXXX",
				"objectId": "4662FB55-37D4-44E8-9713-9CC5004FEA5E"
				[more custom fields ...]
			},
			{
				"lastLogin": 1602715382000,
				"userStatus": "ENABLED",
				"created": 1589919413000,
				"admin": true,
				"socialAccount": "BACKENDLESS",
				"___class": "Users",
				"updated": 1602715394000,
				"email": "xxxxxxxxxxxxxxxxxxx",
				"objectId": "7FD36617-B643-98F4-FF2E-AF86E2CBD900"
				[more custom fields ...]
			},
			{
				"lastLogin": 1602714126000,
				"userStatus": "ENABLED",
				"created": 1590625447000,
				"socialAccount": "BACKENDLESS",
				"___class": "Users",
				"updated": 1602714139000,
				"email": "xxxxxxxxxxxxxx",
				"objectId": "EE62C374-B550-BB3C-FF9B-9D2EC0058A00"
				[more custom fields ...]
			}
		]
	},
	{
		"___class": "notification_groups",
		"group_name": "Invitados",
		"UserId": null,
		"countUsers": 0,
		"objectId": "542793EA-A18C-426D-B9BF-96BC0F6ADA8A",
		"users_related": []
	}
]

Or maybe an array with the objects Ids from all users?

How can I get only specific fields from relation?

Thanks

Hello @Dario_Castaneda

To transform relations/child collections you need to use a afterFind EventHandler and transform the result there

https://backendless.com/docs/bl-js/bl_event_handlers.html

Backendless.ServerCode.Persistence.afterFind('Persons', function(req, res) {
  return res.result.map(person => {
    if(person.orders){
       person.orders = person.orders.map(order => {
         return { objectId: order.objectId }
       })
    }
    
    return person
  })
});

the result you can check in the REST Console

According to load all the object ids, you can load no more than 100 related object for each parent in a single API call, but I believe 100 children should enough

Also, I would recommend you to use custom API Services and inside the service use the loadRelations API

Regards, Vlad

Thank you @vladimir-upirov!

Thank you @vladimir-upirov!