首页 > 解决方案 > 猫鼬 - 人口根本不起作用

问题描述

我有我的帖子集合和帖子有一个硬币属性,它接受硬币文档的 objectID 我想用他们携带的硬币填充我的帖子

这是我的硬币计划

const coinScheme = mongoose.Schema({
    coinName:{
        type:String,
    },
    coinPhoto:{
        type:String
    },
}) 

const Coin = mongoose.model('Coin', coinScheme)

module.exports=Coin

这是我的帖子计划

const postScheme = mongoose.Schema({
     ... // some random properties           
     coin: { // this is the property that I want to populate with Coins
         type: mongoose.Schema.Types.ObjectId, 
         ref:'Coin' 
     }
},{timestamps:true})    

const Post = mongoose.model('Post', postScheme)

module.exports = Post

我通过以下方式尝试了填充->

Post.find().populate('Coin').exec().then(posts=>{
    res.send(posts)
})

/* const posts = await Post.find({})
    await posts.populate('Coin').execPopulate()
    res.send(posts)*/

这是json结果

"numberOfReply": 2,
"numberOfLikes": 0,
"popularity": "0",
"_id": "60832c62f470273eb8e5f82a",
"post": "SELAAM BU COİNTALKZIN İLK POSTUUUU",
"hasImage": true,
"image": "https://i.pinimg.com/280x280_RS/6d/14/85/6d1485278f9cef918a790ae813e997f8.jpg",
"owner": "6082e39b38eec707c8ed3c59",
"isCoin": true, 
"coin": "608331f6e86ba718bcb17db7", // **I wanted to populate this, It sohuld has been populated.**
"createdAt": "2021-04-23T20:21:54.490Z",
"updatedAt": "2021-04-23T20:21:54.490Z",
"__v": 0

标签: node.jsmongodbmongooseexecpopulate

解决方案


推荐阅读