Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose join multiple collections

    var mongoose = require('mongoose');
    var userCollection = require('./user');//import user model file
    var resources = {
    nick_name: "$nick_name",
    email: "$email"};

    userCollection.aggregate([{
            $group: resources
        }, {
            $lookup: {
                from: "Comments", // collection to join
                localField: "_id",//field from the input documents
                foreignField: "user_id",//field from the documents of the "from" collection
                as: "comments"// output array field
            }
        }, {
            $lookup: {
                from: "Post", // from collection name
                localField: "_id",
                foreignField: "user_id",
                as: "posts"
            }
        }],function (error, data) {
         return res.json(data);
     //handle error case also
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: js add key to object 
Javascript :: jquery make visible 
Javascript :: react js download file 
Javascript :: set timeout JS for loop 
Javascript :: how to use foreach in javascript 
Javascript :: get current location url javascript 
Javascript :: jqery get text 
Javascript :: chalk js 
Javascript :: javascript regex example match 
Javascript :: add new items in a select input using js 
Javascript :: get query string javascript nodejs 
Javascript :: forming an object with reduce 
Javascript :: upload files to api using axios 
Javascript :: how to remove class in all siblings javascript 
Javascript :: how to set env variables in js 
Javascript :: local database with javascript 
Javascript :: render object id in an array reactjs from database 
Javascript :: how to merge two objects into one in javascript 
Javascript :: html set textarea value 
Javascript :: duplicate numbers in an array javascript 
Javascript :: js scroll to id on body 
Javascript :: mongoose update and return new 
Javascript :: get all days of month javascript 
Javascript :: javascript get child element by parent id 
Javascript :: javascript pseudo random 
Javascript :: iife js arrow function 
Javascript :: function call ready resize and load 
Javascript :: multiple case switch javascript 
Javascript :: javascript count number of occurrences in array of objects 
Javascript :: how to convert a queryset into json string 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =