今天終於有時間研究
首先我們有兩個 collection
User
var schema = new Schema({
name: {type: String},
gender: {type: String},
old: {type: Number},
books: [{
bookId: {type: ObjectId, ref: 'Book'}
}],
modified: {type: Date, default: Date.now}
});
Book
var schema = new Schema({
name: {type: String},
year: {type: Number},
ISDN: {type: String},
price: {type: Number},
// ownerUserId: [{type: ObjectId, ref: 'User'}],
modified: {type: Date, default: Date.now}
});
使用方法:
db.User.findOne({}).populate({
path: 'books.bookId',
model: 'Book',
select: 'name price',
options: {limit: 2}
}).lean().exec();
不知道這指令跟 mongodb 的 aggregate 有沒有關係...
等我心血來潮的時候再來研究一下囉!!
沒有留言:
張貼留言