title: How to look up into db in mongo db using node js
body: <p>I am learning NodeJS and Mongo Db and I got stuck. Could you please help me If you know solution?</p>
  <p>I have 2 questions</p>
  <p>1.)I have 2 collections in mongo db
  I tried to find out which projects user has. For example michaela has projects :nodejs, test2
  jasmin has projects: test2 (I don't want to know who are coders for current project but I want to know what are projects for current user)</p>
  <p>data what I want to get should look something like this</p>
  <pre><code>[{
    id: &quot;some value&quot;
    username: &quot;michaela&quot;
    email:&quot;some value&quot;
    password:&quot;some value&quot;
    projects:&quot;[{
       project_name:&quot;node js&quot;
       project_description:&quot;learn node js&quot;
       deadline:&quot;2021-11-12&quot;
       status:&quot;todo&quot; 
    },
    {
       project_name:&quot;test2&quot;
       project_description:&quot;learn node js&quot;
       deadline:&quot;2021-11-12&quot;
       status:&quot;todo&quot; 
    }]
    },
    id: &quot;some value&quot;
    username: jasmin&quot;
    email:&quot;some value&quot;
    password:&quot;some value&quot;
    projects:&quot;[
    {
       project_name:&quot;test2&quot;
       project_description:&quot;learn node js&quot;
       deadline:&quot;2021-11-12&quot;
       status:&quot;todo&quot; 
    }]
  
  }]
  
  </code></pre>
  <p>I don't understand exactly how look up works. I tried following example(test) but it doesn't work</p>
  <p>I also don't understand how to connect 2 db for example allMembers and all projects I merged into one array but I don't think this is correct way how to do it.</p>
  <p>this is my model</p>
  <pre><code>const usersCollection = require('../db').db().collection(&quot;users&quot;)
  const projectsCollection = require('../db').db().collection(&quot;projects&quot;)
  
  Team.prototype.getAllMembers = function(){
      return new Promise (async (resolve, reject) =&gt; {
          try{
              let allMembers = await usersCollection.find({}).toArray()
              let allprojects = await projectsCollection.find({}).toArray()
                          let test = await  usersCollection.aggregate([
                  {$lookup: {from: &quot;projects&quot;, localField: &quot;username&quot;, foreignField: &quot;coder&quot;, as: &quot;projects&quot;}},
                  {$project: {
                      username:1, 
                      projects:{
                           name:{$arrayElemAt: [&quot;$projects.project_name&quot;, 0]},
                           description:{$arrayElemAt: [&quot;$projects.project_description&quot;, 0]},
                      },
                      email:1,
                      password:1,
                    }}
                ]).toArray()
                console.log(&quot;test&quot;, test)
              let arr3 = [...allMembers, ...allprojects]
              resolve(arr3)
          }catch{
              reject()
          }
      })
  }
  
  
  </code></pre>
  <p>this is my controller</p>
  <pre><code>exports.home = function(req, res) {
      //when user is log in
      if (req.session.user) {
       let teamMember = new Team(req.body)
        teamMember.getAllMembers().then((data)=&gt;{
          console.log(&quot;members data&quot;, data)
            if(data){
              res.render('members', {data: data })
            }else{
              res.send(&quot;on this page are no data&quot;)
            }
        }).catch((err)=&gt;{
            res.send(&quot;problem&quot;)
        }) 
        //when user is not log in, flash tie errory z db automaticky vymaze po pouziti
      } else {
        res.render('login', {errors: req.flash('errors'), regErrors: req.flash('regErrors')})
      }
    }
  
  
  </code></pre>
  <p>and this is how it looks in db</p>
  <p><a href="https://i.stack.imgur.com/LFjQF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LFjQF.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/puxWJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/puxWJ.png" alt="enter image description here" /></a></p>
  <p>Thank you in advance</p>