node.js - 猫鼬 - 人口根本不起作用
问题描述
我有我的帖子集合和帖子有一个硬币属性,它接受硬币文档的 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
解决方案
推荐阅读
- javascript - 在reactjs中添加项目后更新表而不刷新
- java - 使用 TestNG 框架和 Log4J 为每个测试类保存唯一的日志
- java - 访问网络驱动器时出现 UnknownHostException SmbFile
- python - 如何将结果保存为 csv 并管理列?(Python)
- javascript - Webpack CopyPlugin 修改 JS 文件
- python - Python中的TreeMap - 垂直和水平
- git - 为什么 git push 对一个仓库有效而对另一个仓库无效?git@github.com:权限被拒绝(公钥)
- ios - 如何在核心图形中进行命中检测
- javascript - 如何为许多按钮制作这个 copybutton Javascript?
- numpy - 为什么网格网格有用?特别是,为什么坐标矩阵比其他表示(如简单数组)更有价值?