I am beginner in mean stack development now am trying to get records from two schema by common id but am getting only the records from 2nd schema not from the first schema my code is below
kindly tell me the possible solution to get records from two collections
var mongoose = require("mongoose"),
Schema = mongoose.Schema,
objectId = mongoose.Schema.ObjectId;
var projectSchema = Schema({
name : {type: String, required : true},
designation : {type: String, required : true},
status : {type: Number, required : true},
berief_text : {type: String, required : true},
Projectimage : [{ type: Schema.Types.ObjectId, ref: 'Projectimage' }]
});
var projectsimageSchema = Schema({
projectId : { type: String, ref: 'Project' },
imageLocation : {type: String, required : true }
});
var Projectimage = mongoose.model('Projectimage', projectsimageSchema);
var Project = mongoose.model('Project', projectSchema);
module.exports = {
Projectimage: Projectimage,
Project: Project
};
var express = require("express"),
router = express.Router(),
project = require("../../models/project.js");
var multer = require('multer');
var fs = require('fs');
router.get("/", function(req, res) {
project.Projectimage
.find({ projectId: '58d8f7c2e4b90b0ee461e8f5' })
.populate('Projectimage')
.populate('Project')
.exec(function (err, Projectimage) {
if (err) return res.send(err);
console.log('The are an array: ', Projectimage);
res.send(Projectimage)
});
});
module.exports = router;
This is my output
/* 1 */
{
"_id" : ObjectId("58d8f7c2e4b90b0ee461e8f6"),
"imageLocation" : "uploads\\project_images\\1490614210385download.jpg",
"projectId" : "58d8f7c2e4b90b0ee461e8f5",
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("58d8f7c2e4b90b0ee461e8f7"),
"imageLocation" : "uploads\\project_images\\1490614210390download (3).jpg",
"projectId" : "58d8f7c2e4b90b0ee461e8f5",
"__v" : 0
}
expected output
[{
"_id" : ObjectId("58d8f7c2e4b90b0ee461e8f5"),
"name" : "first project",
"designation" : "chated Accountent",
"berief_text" : "hi welcome to projects",
"status" : 1,
"Projectimage" : [
/* 1 */
{
"_id" : ObjectId("58d8f7c2e4b90b0ee461e8f6"),
"imageLocation" : "uploads\\project_images\\1490614210385download.jpg",
"projectId" : "58d8f7c2e4b90b0ee461e8f5",
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("58d8f7c2e4b90b0ee461e8f7"),
"imageLocation" : "uploads\\project_images\\1490614210390download (3).jpg",
"projectId" : "58d8f7c2e4b90b0ee461e8f5",
"__v" : 0
}
],
"__v" : 0
}
]
this is the output for the first answer
The are an array: [ { _id: 58d8f7c2e4b90b0ee461e8f5,
name: 'first project',
designation: 'chated Accountent',
berief_text: 'hi welcome to projects',
status: 1,
__v: 0,
Projectimage: [] } ]
you should rather do this
project.Project
.findOne({ _id: '58d8f7c2e4b90b0ee461e8f5' })
.populate('Projectimage')
.exec(function (err, project) {
})
you were rather calling a .find
on the Projectimage
subdocument instead of Project
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.