Question: i am using mongodb and nodejs.  

i have 2 collection in database  

1.users  

2.blogs  

both the collections have username as one of the fields on basis of which i want to perform the find operation  

```
router.get('/:username',middleware.isLoggedIn, (req, res) => {
    User.findOne({
        username: req.params.username
    }).then(user1 => {
        res.render('profile/profile_show', {
            user1: user1,
            moment: moment
        })
    })
});

```

i want the above function to perform find operation on users and blogs collection and pass the results in render.how can i do that.

Answer: 
You can use `Promise.all()` to track the results of two separate promises:

```
router.get('/:username',middleware.isLoggedIn, (req, res) => {
    Promise.all([User.findOne(...), Blogs.findOne(...)]).then(([userResult, blogResult]) => {
        // use userResult and blogResult here with res.render()
        res.render(...);
    }).catch(err => {
        console.log(err);
        res.sendStatus(500);
    })
});

```
Agreed with `jfriend00`, it's a way of doing through code, just in case if you want it to be done using one DB call in mongoDB, then have an index on field `username` on both collections which helps to yield faster results, then try this :

**Query :**

```
/** Replace your .findOne() with .aggregate([]) */
Users.aggregate([{ $match: { username: 'myName' } }, {
    $lookup:
    {
        from: "Blogs",
        localField: "username",
        foreignField: "username",
        as: "Blogs"
    }
}])

```

**`Users` Collection :**

```
/* 1 */
{
    "_id" : ObjectId("5e06594d400289966eefb3f9"),
    "username" : "othersName",
    "age" : "Dont know",
    "school" : "who cares"
}

/* 2 */
{
    "_id" : ObjectId("5e065960400289966eefb564"),
    "username" : "myName",
    "age" : "myAge",
    "school" : "mySchool"
}

```

**`Blogs` Collection :**

```
/* 1 */
{
    "_id" : ObjectId("5e065992400289966eefb9a8"),
    "username" : "myName",
    "blog" : "myBlog",
    "details" : "myBlogDetails"
}

/* 2 */
{
    "_id" : ObjectId("5e0659ae400289966eefbc3a"),
    "username" : "othersName",
    "blog" : "not a blog",
    "details" : "useless"
}

```

**Output :**

```
/* 1 */
{
    "_id" : ObjectId("5e065960400289966eefb564"),
    "username" : "myName",
    "age" : "myAge",
    "school" : "mySchool",
    "Blogs" : [ 
        {
            "_id" : ObjectId("5e065992400289966eefb9a8"),
            "username" : "myName",
            "blog" : "myBlog",
            "details" : "myBlogDetails"
        }
    ]
}

```

**Ref :** [$match](https://docs.mongodb.com/manual/reference/operator/aggregation/match/index.html), [$lookup](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/index.html)