jar*_*ibu 2 mongoose mongodb node.js
我尝试与 populate 一起玩但没有成功......有可能做到这一点吗?
我有 2 个 shema : - 用户
import mongoose, { Schema } from 'mongoose'
const userSchema = new Schema({
email: { type: String, unique: true },
password: String,
passwordResetToken: String,
passwordResetExpires: Date,
products: [{
productId: { type: Schema.Types.ObjectId, ref: 'Product' },
dateAdd: { type: Date, default: Date.now }
}]
}, { timestamps: true })
const User = mongoose.model('User', userSchema)
export default User
Run Code Online (Sandbox Code Playgroud)
和产品:
import mongoose, { Schema } from 'mongoose'
const productSchema = new Schema({
domain: String,
originUrl: { type: String },
price: Number,
img: String,
userFollow: [{ type: Schema.Types.ObjectId, ref: 'User' }]
})
const Product = mongoose.model('Product', productSchema)
export default Product
Run Code Online (Sandbox Code Playgroud)
所以我想检索我的每个prodcutId 的所有信息
我尝试这种方式(以及许多其他人没有成功):
User.findOne({ _id: userId }).populate({
path: 'products.productId',
populate: { path: 'products.productId', model: 'Products' }
}).exec(function (err, products) {
if (err) {
console.log('errors :' + err)
}
console.log('Product => ' + util.inspect(products))
})
Run Code Online (Sandbox Code Playgroud)
填充没有效果,只有 findOne() 的结果相同
| 归档时间: |
|
| 查看次数: |
5632 次 |
| 最近记录: |