Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

mongodb - Populating sub-document within same document but different array

let's say I have 2 arrays of objects. One array is 'devices' where each device has its own parameters. Second array is 'cables', where cable is an object with parameters 'from' and 'to' (and some others as well but it is not so important right now). "From" and "to" contains objectIds of devices.

So, this is how my Project document looks

{
_id: 600f327896504a63167a5ac7,
cables: [
{
  fire: false,
  screened: false,
  diameter: '3x4',
  note: 'false',
  _id: 600f2fb001cdd0152cb7fb77,
  powersignal: 'Power',
  from: 600d9d83c9371416a8bd19a9,
  to: 600d9991c9371416a8bd19a8,
  length: 15,
  status: 'Not printed',
  createdAt: 2021-01-25T20:53:04.739Z
},
{
  fire: false,
  screened: false,
  diameter: '4x6',
  note: 'false',
  _id: 600f330333164a1e709fdf6f,
  powersignal: 'Power',
  from: 600d9991c9371416a8bd19a8,
  to: 600d9d83c9371416a8bd19a9,
  length: 45,
  status: 'Not printed',
  createdAt: 2021-01-25T21:07:15.690Z
}
],
adminsID: [],
name: 'B101',
description: 'Project description',
user: 5fff69af08fc5e47a0ce7944,
devices: [
{
  _id: 600d9991c9371416a8bd19a8,
  name: 'UPS',
  number: '6484011',
  loc1: 'PPG',
  loc2: 'Machine Room',
  loc3: '54 LB',
  weight: 50
},
{
  _id: 600d9d83c9371416a8bd19a9,
  name: 'Rozdzielnica g?owna',
  number: '6100001',
  loc1: 'Machine room',
  loc2: 'PPG',
  loc3: '54 LB',
  weight: 700
}

,

As you can see, cables' "from" and "to" refer to objectId of devices. Is there easy way to populate these 2 parameters? I've tried:

     let project = await Project.findById(req.params.id)
    .populate('cables')
    .lean()

but without a success.

Here's my Project schema for reference:

    const mongoose = require('mongoose');

    const ProjectSchema = new mongoose.Schema({
        name: {
            type: String,
            Required: true,
            trim: true
        },
        description: {
            type: String,
            trim: true
        },
        systems: [{
            number: {type: String, required: true},
            name: {type: String, required: true}
        }],

        devices: [{
            name: {type: String, Required: true},
            number: {type: String, trim: true},
            loc1: {type: String, trim: true},
            loc2: {type: String, trim: true},
            loc3: {type: String, trim: true},
            weight: {type: Number, trim: true},
            status: {
                type: String,
                enum: ['Not applicable', 'Checked', 'Prepared', 'Ready'],
                required: true}
        }],

        cables: [
            {
                name: {type: String, required: true, trim: true},
                fire: {type: Boolean, required: true, default: false},
                screened: {type: Boolean, required: true, default: false},
                diameter: {type: String, required: true, default: false},
                note: {type: String, required: false, default: false},
                powersignal: {type: String, enum: [
                    'Power',
                    'Signal',
                    'Power and signal'],
                    required: true},
                from: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'Device',
                    required: true
                },
                to: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'Device',
                    required: true
                },
                length: {type: Number, required: true, min: 0},
                status: {
                    type: String,
                    enum: ['Not printed', 'Printed', 'Laid', 'Connected'],
                    required: true
                },

                system: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'System'
                },
                createdAt: {
                    type: Date,
                    default: Date.now
                }
            }],
            
        user: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        },
        projectUsers: [{
            name: { type: mongoose.Schema.Types.ObjectId,
                    ref: 'projectUsers'},
            level: {type: String,
                enum: ['Management', 'Workman'],
                required: true}
        }],
        createdAt: {
            type: Date,
            default: Date.now
        }
    });

    module.exports = mongoose.model('Project', ProjectSchema);
question from:https://stackoverflow.com/questions/65893370/populating-sub-document-within-same-document-but-different-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...