猫鼬& node js从两个基于id的模式中获取记录

时间:2017-03-27 15:08:40

标签: node.js mongodb express mongoose

我是平均堆栈开发的初学者,现在我试图通过公共ID从两个模式获取记录但是只获取第二个模式的记录而不是第一个模式我的代码在下面

请告诉我从两个馆藏中获取记录的可能解决方案

模型/ project.js

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
};

project.controller.js

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;

这是我的输出

/* 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
}

预期产出

[{
    "_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
}

这是第一个答案的输出

The are an array:  [ { _id: 58d8f7c2e4b90b0ee461e8f5,
    name: 'first project',
    designation: 'chated Accountent',
    berief_text: 'hi welcome to projects',
    status: 1,
    __v: 0,
    Projectimage: [] } ]

1 个答案:

答案 0 :(得分:1)

你应该这样做

project.Project
.findOne({ _id: '58d8f7c2e4b90b0ee461e8f5' })
.populate('Projectimage')
.exec(function (err, project) {

})

您宁愿在.find子文档而不是Projectimage上调用Project