nodejs + mongoose how to make one query need another?

0

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

I'd like it 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 stream you'd like would be Look in the messages table for the id > look for each one if you have dependency > mount the results > check the next check and so on ...

To make a worker ...

    
asked by anonymous 11.11.2018 / 07:23

1 answer

0

I'm not sure if I've gotten pretty well with what you want, but to bring information from a related model, I use it like this in the query:

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

Where my ComicBook is a model. Publisher is the related template

    
16.11.2018 / 14:21