Mongoose fetches records from two related collections with population

I need to know how to search the following collections. Collection id one is used as projectsId in collection two. I gave contoller.js

and the model files below, kindly tell me where should I put the search to get records from the two collections.

collection one:

/* 1 */
{
    "_id" : ObjectId("58dcda850a13352724d4c716"),
    "name" : "senthil",
    "designation" : "CI",
    "berief_text" : null,
    "status" : 1,
    "__v" : 0,
    "type" : 1
}

/* 2 */
{
    "_id" : ObjectId("58dcdade0a13352724d4c719"),
    "name" : "ssss",
    "designation" : "sssss",
    "berief_text" : null,
    "status" : 2,
    "__v" : 0
}

      

collection two:

/* 1 */
{
    "_id" : ObjectId("58dcda850a13352724d4c717"),
    "imageLocation" : "uploads\\project_images\\14908688692032.png",
    "projectId" : ObjectId("58dcda850a13352724d4c716"),
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("58dcda850a13352724d4c718"),
    "imageLocation" : "uploads\\project_images\\1490868869210201876891658ad792131c9520170222051225.JPG",
    "projectId" : ObjectId("58dcda850a13352724d4c716"),
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("58dcdade0a13352724d4c71a"),
    "imageLocation" : "uploads\\project_images\\14908689587042.png",
    "projectId" : ObjectId("58dcdade0a13352724d4c719"),
    "__v" : 0
}

/* 4 */
{
    "_id" : ObjectId("58dcdade0a13352724d4c71b"),
    "imageLocation" : "uploads\\project_images\\14908689587321.png",
    "projectId" : ObjectId("58dcdade0a13352724d4c719"),
    "__v" : 0
}

      

controller.js

var express = require("express"),
 router = express.Router(),
 project = require("../../models/project.js"),
 projectimage = require("../../models/projectimages.js"),
router.get("/", function(req, res) {



 project.find({}, function(err, data){
          if(err){
            res.send(err);
          }
          res.send(data);
        });

  projectimage.find({}, function(err, data){
          if(err){
            res.send(err);
          }
          res.send(data);
        });


})

      

project.js

var mongoose = require("mongoose"),
 Schema = mongoose.Schema,
 objectId = mongoose.Schema.ObjectId;
    var projectSchema = new Schema({
      name    : {type: String, required : true},
      designation    : {type: String, required : true},
      status    : {type: Number, required : true},
      type    : {type: Number, required : true},
      berief_text : {type: String, required : true},
        }); 
    var project = mongoose.model("project", projectSchema);
    module.exports = project;

      

projectimages.js

var mongoose = require("mongoose"),
 Schema = mongoose.Schema,
 objectId = mongoose.Schema.ObjectId;

var ProjectimageSchema = new mongoose.Schema({
    imageLocation: String,
    projectId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'project'
    },

});

var projectimage = mongoose.model("projectimage", ProjectimageSchema);

module.exports = projectimage;

      

got the result

The server is running at: http: // localhost: 3002

[ { _id: 58dcda850a13352724d4c716,
    name: 'senthil',
    designation: 'CI',
    berief_text: null,
    status: 1,
    __v: 0,
    type: 1 },
  { _id: 58dcdade0a13352724d4c719,
    name: 'ssss',
    designation: 'sssss',
    berief_text: null,
    status: 2,
    __v: 0 } ]
[ { _id: 58dcda850a13352724d4c717,
    imageLocation: 'uploads\\project_images\\14908688692032.png',
    projectId: 58dcda850a13352724d4c716,
    __v: 0 },
  { _id: 58dcda850a13352724d4c718,
    imageLocation: 'uploads\\project_images\\1490868869210201876891658ad792131c9520170222051225.JPG',
    projectId: 58dcda850a13352724d4c716,
    __v: 0 },
  { _id: 58dcdade0a13352724d4c71a,
    imageLocation: 'uploads\\project_images\\14908689587042.png',
    projectId: 58dcdade0a13352724d4c719,
    __v: 0 },
  { _id: 58dcdade0a13352724d4c71b,
    imageLocation: 'uploads\\project_images\\14908689587321.png',
    projectId: 58dcdade0a13352724d4c719,
    __v: 0 } ]
[ { _id: 58dcda850a13352724d4c716,
    name: 'senthil',
    designation: 'CI',
    berief_text: null,
    status: 1,
    __v: 0,
    type: 1 },
  { _id: 58dcdade0a13352724d4c719,
    name: 'ssss',
    designation: 'sssss',
    berief_text: null,
    status: 2,
    __v: 0 } ]
[ { _id: 58dcda850a13352724d4c717,
    imageLocation: 'uploads\\project_images\\14908688692032.png',
    projectId: 58dcda850a13352724d4c716,
    __v: 0 },
  { _id: 58dcda850a13352724d4c718,
    imageLocation: 'uploads\\project_images\\1490868869210201876891658ad792131c9520170222051225.JPG',
    projectId: 58dcda850a13352724d4c716,
    __v: 0 },
  { _id: 58dcdade0a13352724d4c71a,
    imageLocation: 'uploads\\project_images\\14908689587042.png',
    projectId: 58dcdade0a13352724d4c719,
    __v: 0 },
  { _id: 58dcdade0a13352724d4c71b,
    imageLocation: 'uploads\\project_images\\14908689587321.png',
    projectId: 58dcdade0a13352724d4c719,
    __v: 0 } ]

      

expected output

[ { _id: 58dcda850a13352724d4c716,
    name: 'senthil',
    designation: 'CI',
    berief_text: null,
    status: 1,
    __v: 0,
    type: 1.
    images:[{ _id: 58dcda850a13352724d4c717,
    imageLocation: 'uploads\\project_images\\14908688692032.png',
    projectId: 58dcda850a13352724d4c716,
    __v: 0 },
  { _id: 58dcda850a13352724d4c718,
    imageLocation: 'uploads\\project_images\\1490868869210201876891658ad792131c9520170222051225.JPG',
    projectId: 58dcda850a13352724d4c716,
    __v: 0 }] 
},

]

      

my for each loop

 project.find({}, function(err, data){
          if(err){
            res.send(err);
          }

for each (var v in data) {
    console.log(v);
}
        });

      

+3


source to share


1 answer


Use population for reference data. While your link is defined in yours ProjectimageSchema

, you also need to add the link to projectsSchema

. You can rename the ref property projectId

to ProjectimageSchema

to project

to make it clearer.

Project outline

var projectSchema = new Schema({
    name: {type: String, required : true},
    designation: {type: String, required : true},
    status: {type: Number, required : true},
    type: {type: Number, required : true},
    berief_text: {type: String, required : true},
    images: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Projectimage'
    }
});

      

ProjectImages schema



var ProjectimageSchema = new mongoose.Schema({
    imageLocation: String,
    project: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'project'
    }
});

      

Once you do that, you can get the related data + related data with padding:

//get all project with relates images
project.find()
    .populate('images')
    .exec(function (err, post) {
        if (err) return handleError(err);
    });

//get all project images with relates projects
projectimage.find()
    .populate('project')
    .exec(function (err, post) {
        if (err) return handleError(err);
    });

      

+1


source







All Articles