0%
MongoDB和Node(三)
联系:多表查询
一,使用id对集合进行关联
使用populate方法进行关联集合查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const Userlj = mongoose.model('Userlj', new mongoose.Schema({ name: { type: String } }))
const Postlj = mongoose.model('Postlj', new mongoose.Schema({ title: { type: String }, author: { type: mongoose.Schema.Types.ObjectId, ref: 'Userlj' } }))
Userlj.create({ name: 'dyy' }).then(res => { console.log(res) })
Postlj.create({ title: 'love', author: '61552d2f1248537d84a065b1' }).then(res => { console.log(res) })
Postlj.find().populate('author').then(res => { console.log(res) })
|