I have two schemas:
const categorySchema = Schema({
slug: {
type: String,
index: true
},
tags: {
type: [Schema.Types.ObjectId],
ref: 'Tag'
}
});
and:
const tagSchema = Schema({
title: String,
type: {
type: String,
enum: ['bool', 'selectable', 'int']
},
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],
possibleValues: [String]
});
Now here is the problem. When I try to populate my category instance with tags, the whole field, becomes an empty array, while when there are no .populate() statements, there are some ObjectIds there. What is the problem?
Update: here is my query:
models.Category
.findOne({_id: req.params.categoryId})
.populate('tags')
.then(category => {
console.log(category);
res.send(category.tags);
});
Category Schema :
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var categorySchema = Schema({
slug: {type: String,index: true},
tags: { type: [Schema.Types.ObjectId],ref: 'Tag'}
},{
collection:'categories'
});
var Category = mongoose.model('Category', categorySchema);
Tag Schema :
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var tagSchema = Schema({
title: String,
type: {
type: String,
enum: ['bool', 'selectable', 'int']
},
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],
possibleValues: [String]
},{
collection:'tags'
});
var Tag = mongoose.model('Tag', tagSchema);
Find Query :
Category.
findOne({_id: req.params.categoryId}).
populate('tags').
exec(function (err, categories) {
return res.json(categories);
});
I think you need to change the schema definition of category.tags to this:
tags: [{
type: Schema.Types.ObjectId,
ref: 'Tag'
}]
Related
I want to reference to a subdocument which is defined as a schema. Here's the example:
exam.model.js:
const answerSchema = new mongoose.Schema({
text: String,
isCorrect: Boolean,
});
const questionSchema = new mongoose.Schema({
text: String,
answers: [answerSchema],
});
const examSchema = new mongoose.Schema({
title: String,
questions: [questionSchema],
})
const ExamModel = mongoose.model("Exam", examSchema);
//...export the schemas and ExamModel...
solvedExam.model.js:
const solvedExamSchema = new mongoose.Schema({
exam: {
type: mongoose.Schema.Types.ObjectId,
ref: "Exam",
}
answers: [{
question: {
type: mongoose.Schema.Types.ObjectId,
ref: //What do I put here? "question" is not a model, it's only a sub-document schema
}
answer: {
type: mongoose.Schema.Types.ObjectId,
ref: //Same problem
}
}],
});
So as it's obvious, I want to reference to the questions and answers which are only subdocuments as a schema and NOT models. How can I reference them? Thanks.
You should declare the respective Answer and Question Schema and refence those:
const answerSchema = new mongoose.Schema({
text: String,
isCorrect: Boolean,
});
const questionSchema = new mongoose.Schema({
text: String,
answers: [answerSchema],
});
const examSchema = new mongoose.Schema({
title: String,
questions: [questionSchema],
})
const AnswerModel = mongoose.model("Answer", examSchema);
const QuestionModel = mongoose.model("Question", examSchema);
const ExamModel = mongoose.model("Exam", examSchema);
...
const solvedExamSchema = new mongoose.Schema({
exam: {
type: mongoose.Schema.Types.ObjectId,
ref: "Exam",
}
answers: [{
question: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Question'
}
answer: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Answer'
}
}],
});
assuming i have this 2 schemas
company schema with an array of categories
//category
export const CategorySchema = new mongoose.Schema({
name: { type: String },
}, { timestamps: true });
//company
export const CompanySchema = new mongoose.Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
name:String,
email:String,
categories:{type: [CategorySchema], default: [] },
}, { timestamps: true });
product schema with category as a ref to the category from company
export const ProductSchema =new mongoose.Schema({
name:String,
category:{ type: Schema.Types.ObjectId, ref: 'Category' },
}, { timestamps: true })
is it possible to populate category from the product ?
i tried this code and it's not working
const products=await this.productModel.find({}).populate({'path':"category","model":"Category"}) ``
what you need is just
productModel.find({}).populate("category")
try this code for populate
const products=await this.productModel.find({}).populate({'path':"category","model":"company.categories"})
How can I populate category in courses model?
I have courses, courses will have category from categories->subcategories model.
I don't know how to populate from nested objects.
Reference model is category, I have to populate from array subcategories!
**courses:**
const CourseSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
name: {
type: String
},
description: {
type: String
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'category'
}
});
**category:**
const mongoose = require("mongoose");
const CategorySchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
name: {
type: String,
required: true
},
subcategories: [{
name: {
type: String
}
}]
});
const Category = mongoose.model('category', CategorySchema);
module.exports = Category;
use seprate collection for subCetagory like:
**courses**
const CourseSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
name: {
type: String
},
description: {
type: String
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'SubCategory'
}
});
**SubCategory:**
const mongoose = require("mongoose");
const SubCategorySchema = new mongoose.Schema({
name: {
type: String,
required: true
},
categorie: {
type: String,
ref: "category"
}
});
const SubCategory = mongoose.model('category', SubCategorySchema);
module.exports = SubCategory;
**Category**
const mongoose = require("mongoose");
const CategorySchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
name: {
type: String,
required: true
},
subcategories: [
{
type: Schema.types.objectId,
ref: "SubCategory"
}
]
});
then just add subcetagory id to course when insert new course or update a course and when you want to get course along with subcetagory and category use populate query of mongoose
currently i created a schema for storing products using mongoose as below
const Schema = mongoose.Schema;
const ProductSchema = new Schema({
title: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
description: {
type: String,
required: true
},
quantity: {
type: Number,
required: true
},
manufacture: {
type: String,
required: true
},
creator: {
type: Schema.Types.ObjectId,
ref: 'user'
},
category: {
type: Schema.Types.ObjectId,
ref: 'category'
}
});
module.exports = { Product: mongoose.model('product', ProductSchema) };
and here another schema for storing categories that products are related to
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
title: {
type: String,
required: true
}
});
module.exports = { Category: mongoose.model('category', CategorySchema) };
each product is related to a category
the question is how can i display all products that are related to a specific category.
i tried .find() method but i can't use it correctly.
thanks for any advice
You need to use .populate('category'), after .find() in order to populate all the connected data.
products.find().populate('category').exec((err, product) => { })
The field (viewed_posts) i want to populate in User Schema:
viewed_posts: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Viewed"
}
]
Viewed Schema :
var viewedSchema = new mongoose.Schema({
hitsByUser: {type: Number, default: 0},
viewsByUser: {type: Number, default: 0},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post"
}
});
Post Schema :
var mongoose = require('mongoose');
var passportLocalMongoose = require("passport-local-mongoose");
var uniqueValidator = require('mongoose-unique-validator');
var marked = require('marked');
var slugify = require('slugify');
// this is done for sanitizing html so that user cannot write a script in the input
const createDomPurify = require('dompurify')
const {JSDOM} = require('jsdom')
const dompurify = createDomPurify(new JSDOM().window)
var postSchema = new mongoose.Schema({
postNumber: Number,
title: String,
content: String,
subject: String, // currently we have 4 subjects so one out of 4 subjects will be stored here
likes: {type:Number,default:0},
// likes: {
// id:{
// type: mongoose.Schema.Types.ObjectId,
// ref: "User"
// }
// },
views: {type:Number,default:0},
actualViews: {type:Number,default:0},
shares: Number,
isReviewedByAdmin: {type: Boolean, default: false},
isReviewedByAuditor: {type: Boolean, default: false},
author: {
id:{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: {
type: String
}
},
publish_date: {
type: String,
default: Date.now
},
publishDay: String,
slug: {
type: String,
required: true,
},
sanitizedHtml: {
type: String,
required: true
},
imagename: String //2002-12-09
});
I wish to see whole structure printed , but i can only populate viewed_posts, how can i populate "post"
which is inside viewed Schema and see here:
User.findById(req.user._id).populate("viewed_posts").exec((err,ans)=>{
if(err) console.log(err)
else{
console.log("this is the answer ",ans)
}})
The output i get:
},
{
hitsByUser: 0,
viewsByUser: 0,
_id: 5f9e85aeec37700f54a4d029,
post: 5f9a93d38d7cf8544ce9cc21,
__v: 0
},
{
hitsByUser: 0,
viewsByUser: 0,
_id: 5f9e85d61841000478c85f8a,
post: 5f82773f1998150024d4c8fc,
__v: 0
},
But i expect this post to be expanded too, instead of just showing id , How can i achieve it. Any Help Would be appreciated.
Mongoose supports nested populating (see in the docs: https://mongoosejs.com/docs/populate.html#deep-populate).
Note that you have to specify your model name of post schema where I´ve put the "post-model-name" placeholder.
So you could try something like this:
User.findById(req.user._id)
.populate({
path: 'viewed_posts',
populate: {
path: 'post',
model: 'post-model-name'
}
})
.exec();