nodejs + Mongoose how do for one query need another?

Asked

Viewed 460 times

1

I have two tables they have a link id being table A(Initial) tableB(dependencies) however there may be dependencies.

I’d like you to just return in the dependency sequence.

function Deps(dt) {
  return new Promise((resolve,reject) => {
  msg_deps.find({deps: dt},function (err, dts) {
    if (err) return console.error(error);
    dts.forEach(function(el) {
      return resolve(el)
    })

  })
})
}

var Result = []
  msg.find(function (err, dts) {
    if (err) return console.error(error);


        dts.forEach(function(el) {
          //Aqui coloca o primeiro resultado no novo array
          Result.push(el);  
          // Aqui chama a função para colocar na seq. as dependencias
          // Mais não consigo acrescentar para um novo array, ou nao fica na sequencia..
          AddDeps(el).then(res => Result.push(res);

        })
  });

Dados da tabela no mongo:

Msg
[ { _id: 5be65f74111ae2540c1f3c22,
    userId: '001',
    text: 'primeiro teste',
    creatAt: 2018-11-10T04:32:52.462Z,
    __v: 0 },
  { _id: 5be660416efb425477aedf3a,
    userId: '001',
    text: 'segundo teste',
    creatAt: 2018-11-10T04:36:17.033Z,
    __v: 0 },
  { _id: 5be674bacfaa97646e14cdce,
    userId: '001',
    text: 'dependencia segundo teste',
    creatAt: 2018-11-10T06:03:38.419Z,
    __v: 0 },
  { _id: 5be67677fef0e46563802c41,
    userId: '001',
    text: 'deps segundo teste',
    creatAt: 2018-11-10T06:11:03.558Z,
    __v: 0 } ]

msg_deps
[ { _id: 5be65f74111ae2540c1f3c22,
    userId: '001',
    deps: '5be67677fef0e46563802c41'
    text: 'deps primeiro teste',
    creatAt: 2018-11-10T04:32:52.462Z,
    __v: 0 },
  { _id: 5be660416efcccc477aedf3y,
    deps: '5be67677fef0e46563802c41'
    userId: '001',
    text: 'segundo teste',
    creatAt: 2018-11-10T04:36:17.033Z,
    __v: 0 },
  { _id: 5be674bacfaa97646e14cdce,
    userId: '001',
    deps: '5be67677fef0e46563802b43'
    text: 'dependencia segundo teste',
    creatAt: 2018-11-10T06:03:38.419Z,
    __v: 0 },
  ***{ _id: 5be67677fef0e46563802c40,
    deps: '5be660416efb425477aedf3y'
    userId: '001',
    text: 'deps segundo teste',
    creatAt: 2018-11-10T06:11:03.558Z,
    __v: 0 } ]*** // aqui este registro tem dependencia na propria tabela de dependencia

The flow I’d like would be Search the messages table for id > search each one for dependency > mount results > check next one and so on...

To make a worker...

1 answer

0

I’m not sure if I understand very well what you want, but to bring the information of a related model, I usually use so in consultation:

ComicBook.aggregate([
{
  $lookup: {
    from: "publisher",
    localField: "publisher_id",
    foreignField: "_id",
    as: "publisher"
  }
}])

Where my Comicbook is a model. Publisher is the related model

Browser other questions tagged

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