Relationship with Mongodb

Asked

Viewed 56 times

0

I am using populate to create relationship with mongoDB, in case I have clients and projects, both belonging to the logged-in user, and being the project belonging to a client, my doubt is that to relate the client with the user was quiet, because there was no other relationship with the client, already the project that is already related to the client I got lost a little.

My Dao of the project

       async getAll(){ 
           return await this._base._model.find().populate('cliente', '_id nome email tel endereco cidade bairro cep');
       }

By already having getAll with the customer relationship, how could I add the user as well as is in my client’s DAO. If I create another get it would work ?

Meu model do projeto:

const projetoModel = new schema({
    //nomeCliente: {trim: true, required: true, type: String, index: true},
    //tel: {trim:true, requried: true, type: String},
    //email: {type: String},
    //endereco: {type: String, required: true},
    //bairro: {type: String, required: true},
    //cidade: {type: String, required: true, index: true},
    tituloProjeto: {trim: true, required: true, index: true, type: String },
    descricaoProjeto: {type: String, required: true},
    //cep: {type: String, required: true},
    preco: {type: String},
    foto: {type: Array},
    status: {type: Boolean, default: true},
    data: {type: Date, default: new Date()},
    cliente: {type: schema.Types.ObjectId, ref: 'Clientes'}

}

Dao client:

 async getAll(){ 
        return await this._base._model.find().populate('usuario', '_id nome email');
    }
  • Take a look at the mongodb’s manual on Collection: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/index.html

  • 1

    I’ve got it, brother, thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.