0

I want to get only all messages related to the projects posted by user. I don’t think there is a way to do so using populate because we have to get the user id from the other collection and then render messages using his id which is "submittedBy" in the project schema.

I am trying aggregator but it is also not working.

Any help or clue would be appreciated.

message Schema:

const MessageSchema = new Schema({
content:{
    type: String,
    trim: true
},
projectId:{
    type: Schema.Types.ObjectId,
    ref: 'Form'
},
imageId:{
    type: Schema.Types.ObjectId,
    ref: 'Image'
},
sender:{
    type: Schema.Types.ObjectId,
    ref: 'User'
}, 
readBy:[{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }],
}, {timestamps:true});

Project schema:

const FormSchema = new Schema({
submittedBy: {
    type: Schema.Types.ObjectId,
    ref: 'User'
},
logoName: {
    type: String,
    trim: true
},
industry: [{
    type: String,
    trim: true
}],
logoType: [{
    type: String,
    trim: true
}],
colors: [{
    type: String,
    trim: true
}],
tagline: {
    type: String,
    trim: true
},
org: {
    type: String,
    trim: true
},
communicate: {
    type: String,
    trim: true
},
deadline : {
    type: String,
    trim: true
},
services: {
    type: String,
    trim: true
},
package: {
    type: String,
    trim: true
},
assignedTo: {
    type: String,
    trim: true
   }
}, {timestamps:true})

I tried population but I don’t think it will work or efficient.

So I am using aggregation but it’s not working:


var userId = req.session.user._id;

        results = await Model.aggregate([
            
            {
              lookup:                 {                   from: "forms",                   localField: "projectId",                   foreignField: "_id",                   as: "tags"                 }            },            {unwind: "tags" },            {match:{ 'tags.submittedBy': userId }},
           

         ]).exec(function(err, results) {
            console.log(results)
        });

Result After Combining two collections:

{
_id: 609bd3772583c56dd3e302d3,
readBy: [],
content: 'Remove Tagline and align.',
sender: 604db5708cf232652cd4469e,
imageId: 6099964c2583c56dd3e302c3,
projectId: 60968eed2583c56dd3e302ac,
createdAt: 2021-05-12T13:09:11.060Z,
updatedAt: 2021-05-12T13:09:11.060Z,
__v: 0,
tags: {
  _id: 60968eed2583c56dd3e302ac,
  industry: [Array],
  logoType: [Array],
  colors: [Array],
  logoName: 'global consultancy services',
  tagline: 'Follow your dreams',
  org: 'We guide students who seeks to study overseas e.g. the UK. We offer consultancy services to students from selecting a university to getting visa and then getting in the country.',
  communicate: '3 conceptsrn' +
    'Keep "Indra global " big AND boldrn' +
    'and "consultancy services" below it.rn' +
    'In one concept you can use IGCS initials to create icon form'
  deadline: '2021-05-12',
  services: '',
  package: 'undefined',
  submittedBy: 60968eed2583c56dd3e302ab,
  createdAt: 2021-05-08T13:15:25.661Z,
  updatedAt: 2021-05-10T08:28:18.587Z,
  __v: 0,
  assignedTo: '6050d8485cc8bb6a20ef36fa'
}

} ]

john Asked question May 13, 2021