How to add reference of different field?

I have two tables 1. Users 2. Products
And I want to manage one array named as “viewUsers”. I want to give a user table reference in this array field but the problem is we are not using MongoDB auto-generated _id field. The productId we are considering is having a field name as “id”.

So there is any way we can specify the field name while refencing?
I have tried below in my model:
viewUsers: { type: [{ type: Number, ref: ‘users’, field: “id” }] }
But it’s not working.

You might not be using the

but all documents do have one and it is unique. Nothing stops you from using this _id for the ref. despite the presence of another id field.

Why using another field name? Nothings stops you from using _id for your productId. The _id field will be there, use it or not, will take some space use it or not, will have an unique index will updated when inserting, use it or not.

@steevej , yeah you are right we can use _id but from starting we are managing id in this project so I got your point their is no way to define custom field & it will point to _id by default right?

This is not really what I meant. My point was why are you trying to use another one when the one that is there by default (and potentially optimized) is sufficient.

The following makes me think that you must be using something like mongoose.

I don’t know mongoose. With simple $lookup you may use what ever fields you which to refer to another object. If you are indeed using mongoose, tag your post accordingly so that mongoose people can see it better.

Yes, I’m using mongoose. I got your concern @steevej. Thank you