I am making an application in which I allow users to follow other users. Therefore I have a User collection:
name:{
type: String,
unique: false,
require: [true, 'El nombre es requerido']
},
email:{
type: String,
unique: true,
require: [true, 'El email es requerido']
},
password:{
type: String,
require: [true, 'El password es requerido']
},
public_date:{
type: Date,
required: [true, 'La fecha de creacion es requerida']
},
update_date:{
type: Date,
required: false
},
state:{
type: Boolean,
require: [true, 'El estado es requerido']
}
And a Tracing collection:
follower:{
type: Schema.Types.ObjectId,
ref: 'Usuario',
require: true
},
following:{
type: Schema.Types.ObjectId,
ref: 'Usuario',
require: true
},
public_date:{
type: Date
}
The problem I have is that I can enter multiple documents with the same following and follower values as other documents, therefore I have that the same user can be "following" the same user several times.
In SQL you can use following and follower as primary key, my question is, can something similar be done here?
PD: I have read that you can create an Object with the following and follower attributes and define this object as unique, I did that test but I have problems when returning all the documents of a follower.
the type of index you need is known as a composite index.
What you would have to do is create the index as follows:
In this way, the index that will be generated will group the data first by the follower field and then by the following field.
I hope you find this information useful