MongoDb unable to populate user

Multi tool use
MongoDb unable to populate user
Hi I am trying to populate user into another schema called feedbackschema.
Feedback schema
const mongoose = require('mongoose');
const {
Schema,
} = mongoose;
// Create Schema
const FeedbackSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users',
},
pro: {
type: String,
required: true,
},
con: {
type: String,
required: true,
},
comments: {
type: String,
required: true,
},
rating: {
type: String,
required: true,
},
});
// Create model
const feedback = mongoose.model('feedbacks', FeedbackSchema);
module.exports = feedback;
User Schema
const mongoose = require('mongoose');
const {
Schema,
} = mongoose;
// Create Schema
const UserSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
required: true,
default: false,
},
});
// Create a model
const user = mongoose.model('users', UserSchema);
// Export the model
module.exports = user;
and here is my controller where I am trying to populate the user
getAllFeedbacks: async (req, res) => {
const errors = {};
try {
const feedbacks = await Feedback.find().populate('user');
return res.json(feedbacks);
} catch (err) {
errors.noFeedbacks = 'Please try again';
return res.status(404).json(errors);
}
},
Json I am receiving through postman is this
[
{
"_id": "5b3adf88f3c4cd836bdc2eda",
"pro": "knfklngfdklgnfdgknkln",
"con": "Sales executive updates",
"comments": "This is a another funfact for me is me too",
"rating": "8",
"__v": 0
}
]
It is supposed to show user key but somehow its not working. I checked the current user data is already there but for some reason its not pushing the user info feedback object.
FeedBack collection
[
{
"_id": {
"$oid": "5b3adf88f3c4cd836bdc2eda"
},
"pro": "knfklngfdklgnfdgknkln",
"con": "Sales executive updates",
"comments": "This is a another funfact for me is me too",
"rating": "8",
"__v": 0
}
]
User Collection
[
{
"_id": {
"$oid": "5b37e456565971258da97d5e"
},
"isAdmin": false,
"name": "montygoldy",
"email": "montygoldy@gmail.com",
"password": "$2a$10$zWbxV0Q3VPUxRC6lzJyPBec3P/8zYBaSCTJ2n88Uru3zzFlicR2rq",
"__v": 0
}
]
@AnthonyWinzlet updated the post
– MontyGoldy
Jul 3 at 3:00
I need you sample collections... Please copy it from mlab and paste it here... Please don't paste screen shots
– Anthony Winzlet
Jul 3 at 3:41
Feedback collection { "_id": { "$oid": "5b3adf88f3c4cd836bdc2eda" }, "pro": "knfklngfdklgnfdgknkln", "con": "Sales executive updates", "comments": "This is a another funfact for me is me too", "rating": "8", "__v": 0 }
– MontyGoldy
Jul 3 at 3:43
user collection{ "_id": { "$oid": "5b37e456565971258da97d5e" }, "isAdmin": false, "name": "montygoldy", "email": "montygoldy@gmail.com", "password": "$2a$10$zWbxV0Q3VPUxRC6lzJyPBec3P/8zYBaSCTJ2n88Uru3zzFlicR2rq", "__v": 0 }
– MontyGoldy
Jul 3 at 3:43
1 Answer
1
You can try below aggregation
Feedback.aggregate([
{ "$lookup": {
"from": User.collection.name,
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$_id", mongoose.types.ObjectId(loggedInUserId) ] } } }
],
"as": "user"
}},
{ "$unwind": "$user" }
])
Please change User.collection.name
with your corresponding user collections name... Which might be user
or users
User.collection.name
user
users
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you please show your sample collection copying from mongo shell or robomongo
– Anthony Winzlet
Jul 3 at 2:58