angularjs - Mongoose 从两个相关集合中获取记录并填充

标签 angularjs node.js express mean-stack

我需要知道如何在以下集合中执行查找操作。 集合一的 id 在集合二中用作 projectsId。我已经给出了 contoller.js 和下面的模型文件,请告诉我我必须在哪里放置查找以从两个集合中获取记录

集合一:

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

集合二:

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


})

项目.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;

项目图像.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;

获得输出

服务器运行于: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 } ]

预期输出

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

]

我的每个循环

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

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

最佳答案

使用population以获取引用数据。虽然您的引用目前在您的 ProjectimageSchema 中定义,但您还必须向 projectsSchema 添加一个引用。您可以将 ProjectimageSchema 中的 ref 属性 projectId 重命名为 project 以使其更清晰。

项目架构

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 架构

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

完成此操作后,您可以使用填充获取相关数据 + 相关数据:

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

关于angularjs - Mongoose 从两个相关集合中获取记录并填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43154202/

相关文章:

javascript - 在 Node.js 中的 res.render() 之后使用 server-sent-events

node.js - 如何使用 Mongoose 验证字符串长度?

node.js - Mozilla Firefox 网络选项卡中已传输和大小列之间的区别

angularjs - Angular Directive 的模板绑定(bind)不会更新

javascript - ngRepeat 中复杂的 HTML 结构与深 Scope 对象使事情变慢

javascript - Node 表达 JSON-Schema 多字段验证

javascript - 如何使用express.js和request.js从Amazon S3下载文件?

html - 将焦点状态添加到具有嵌入式链接的容器

javascript - ng-click 和 ng-show 不适用于动态内容( Angular )

node.js - Express 中间件 protected 和不 protected 路由