[英]How can i populate multiple paths in my DB?
I want different values from the nested schema.我想要来自嵌套模式的不同值。 How can I populate them so that every field is showing me its nested data and not the object ID?如何填充它们以便每个字段都向我显示其嵌套数据而不是对象 ID? I'm using MongoDB and node/express.我正在使用 MongoDB 和 node/express。
This is my postDB where the post schema is defined:这是我的 postDB,其中定义了 post 架构:
const mongoose = require('mongoose'); var postSchema = new mongoose.Schema({ title: { type:String, required:true }, body: { type:String, required:true }, comments:[{ type: mongoose.Schema.Types.ObjectId, ref: "comment" }], category:{ type:String, required:true }, creator: { type: mongoose.Schema.Types.ObjectId, ref: "user" } },{timestamps : true} ) module.exports = mongoose.model('postData', postSchema);
This is my commentDB which is referenced from the postDB:这是我从 postDB 引用的 commentDB:
const mongoose = require('mongoose'); // Using the Schema constructor, create a new CommentSchema object // This is similar to a Sequelize model var CommentSchema = new mongoose.Schema({ // `body` is of type String creator: { type: mongoose.Schema.Types.ObjectId, ref: "user" }, body: String },{timestamps : true}); var Comment = mongoose.model("comment", CommentSchema); module.exports = Comment;
This is how I'm trying to populate:这就是我试图填充的方式:
router.get('/READ', (req,res)=>{ posts.find({}, function (err, post) { if (err) { console.log(err); }else{ res.json({post}) } } ) .populate([{path:'creator'}, {path:'comments'}]) })
However the results i get from this does not populate every object ID.然而,我从中得到的结果并没有填充每个对象 ID。
For example:例如:
{ "comments": [ { "_id": "5f8d91d8f8550044f0f755c8", "creator": "5f84e5b1d893ac42dcc9cb78", "body": "This looks cool", "createdAt": "2020-10-19T13:17:12.323Z", "updatedAt": "2020-10-19T13:17:12.323Z", "__v": 0 }, { "_id": "5f8d92e82ecfbe34b8f6375b", "creater": "5f84e5b1d893ac42dcc9cb78", "body": "hello", "createdAt": "2020-10-19T13:21:44.463Z", "updatedAt": "2020-10-19T13:21:44.463Z", "__v": 0 }, ], "_id": "5f887cef6fd7d34548a592ea", "title": "A DESCRIPTIVE PARAGRAPH EXAMPLE", "body": "\\"The room in which I found myself was very large and lofty. The windows were ", "category": "Finance", "creator": { "joined": "2020-10-15T12:14:23.888Z", "posts": [ "5f887cef6fd7d34548a592ea", "5f887e0d6fd7d34548a592ec", "5f887e266fd7d34548a592ed", "5f887e586fd7d34548a592ee", "5f89bfccc2bebd40b07b044a", "5f89c36e906bbb27b84af897", "5f89c7614199d52b141ff249", "5f89c7ea4199d52b141ff24a", "5f8c5ab175ef762ed89eddba", "5f8c5be2d7fac046f0021d9f" ], "_id": "5f88481d00ed460960da90f8", "username": "kenwaysharma", "email": "kenwaysharma@gmail.com", "password": "$2b$10$p3qjmdSKWIF9qAagZoqbjuG34cjOgXTe5XYER0aowwviIS65COVlu", "__v": 0 }, "__v": 0, "updatedAt": "2020-10-20T05:42:56.320Z" }
Here is the userDB:这是用户数据库:
username: { type: String, required: [true, "Username is required!"], unique: true, lowercase: true, }, email:{ type: String, required: [true, "Email is required!"], unique: true, lowercase: true, validate: [isEmail, "Please enter a valid email!"] }, password:{ type: String, required: [true, "Password is required!"], minlength: [6, "Enter atleast 6 characters!"], }, comments:[{ type: mongoose.Schema.Types.ObjectId, ref: "comment" }], posts:[{ type: mongoose.Schema.Types.ObjectId, ref: "postData" }], },{timestamps : true});
GET users :获取用户:
router.get('/USERS', (req,res)=>{ User.find({}, function (err, user) { if (err) { console.log(err); }else{ res.send(user) } } ).populate('comments') .populate('posts') })
How do I get the creator data inside of comments instead of just its object ID?如何在评论中获取创建者数据,而不仅仅是其对象 ID?
Update: I also tried selecting the creator inside comments like .populate('comments', 'creator') but it still gives me the creator object ID in a string.更新:我也尝试在像 .populate('comments', 'creator') 这样的评论中选择创建者,但它仍然给我一个字符串中的创建者对象 ID。
Update 2: I have added the code for userDB to which the commentDB and postDB references.更新 2:我添加了注释数据库和 postDB 引用的 userDB 代码。 Also added the GET users just to see how it works in postman.还添加了 GET 用户只是为了看看它在邮递员中是如何工作的。
Try chaining multiple populate methods and using the exec method to pass your callback.尝试链接多个填充方法并使用 exec 方法传递您的回调。
posts.find({})
.populate({
path: 'comments',
populate: {
path: 'creator',
model: 'user'
}
})
.populate('creator')
.exec(function (err, post) {
if (err) {
console.log(err);
}else{
res.json({post})
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.