参考多个文件Mongoose
我试图用mongoose连接3个不同的文件(主要是学习如何使用它),我已经build立了3个不同的模式,如下所示:
(所有这些都在自己的文件中)
const Books = new Schema({ title: { type: String, required: true, unique: true }, description: String, author: { type: mongoose.Schema.Types.ObjectId, ref: 'Authors' }, stores: [{ available: Number, store: { type: mongoose.Schema.Types.ObjectId, ref: 'Stores' } }] }); exports.Books = mongoose.model('Books', Books); const Authors = new Schema({ name: { type: String, required: true }, description: String, born: Date, died: { type: Date, default: null } }); exports.Authors = mongoose.model('Authors', Authors); const Stores = new Schema({ name: { type: String, required: true, unique: true }, description: String, address: String }); exports.Stores = mongoose.model('Stores', Stores);
然后我添加以下数据:
author.name = 'Jonathan Swift'; author.description = 'old satiric bloke'; author.born = new Date(1667, 10, 30); author.died = new Date(1745, 9, 19); author.save() .catch((err) => console.log(err)) .then(() => console.log('author saved')); store.name = 'Book shop 1'; store.description = 'This is where the cool kids buy all there books!'; store.address = 'La La Land, 123 Combaja street'; store.save() .catch((err) => console.log(err)) .then(() => console.log('store saved')); book.title = 'gullivers travels'; book.author = '58a79345711f2350999e0911'; // _id from the author save book.description = 'A book about a big guy in a small world'; book.stores = [{ available: 8, store: '58a79345711f2350999e0912' // _id from the store save }]; book.save() .catch((err) => console.log(err)) .then(() => console.log('store saved'));
我发现的问题是,当我运行book.find()
它会返回:
[ { "_id": "58a795b8298b50527412815c", "description": "A book about a big guy in a small world", "author": { "_id" : "58a79345711f2350999e0911", "born" : -9532947600000, "died" : -7075130400000, "description" : "old satiric bloke", "name" : "Jonathan Swift", "__v" : 0 }, "title": "gullivers travels", "__v": 0, "stores": [ { "available": 8, "store": "58a79345711f2350999e0912", "_id": "58a795b8298b50527412815d" } ] } ]
我期待/希望得到的是整个商店,就像作者一样,我错过了什么,或者我应该如何去实现预期的结果? 我试过populate
但没有成功
在您的Books
模型的author
& stores.store
是引用,如果您不使用populate()
则不应该填充author
字段。
如果您直接指定刚刚创build的商店和作者的_id
:
var author = new Authors({ name: 'Jonathan Swift', description: 'old satiric bloke', born: new Date(1667, 10, 30), died: new Date(1745, 9, 19) }); author.save(); var store = new Stores({ name: 'Book shop 1', description: 'This is where the cool kids buy all there books!', address: 'La La Land, 123 Combaja street' }); store.save(); var book = new Books({ title: 'gullivers travels', author: author._id, // _id from the author save description: 'A book about a big guy in a small world', stores: [{ available: 8, store: store._id // _id from the store save }] }) book.save();
Books.find()
给出了预期的结果:
[ { "_id": "58a7a2529a8b894656a42e00", "title": "gullivers travels", "author": "58a7a2529a8b894656a42dfe", "description": "A book about a big guy in a small world", "__v": 0, "stores": [ { "available": 8, "store": "58a7a2529a8b894656a42dff", "_id": "58a7a2529a8b894656a42e01" } ] } ]
如果您要stores.store
和author
,请使用:
Books.find().populate([{path:'author'},{path:'stores.store'}]).exec(function(err, res) { console.log(JSON.stringify(res, null, 2)); });
它给出了预期的author
和stores.store
都填充:
[ { "_id": "58a7a2529a8b894656a42e00", "title": "gullivers travels", "author": { "_id": "58a7a2529a8b894656a42dfe", "name": "Jonathan Swift", "description": "old satiric bloke", "born": "1667-11-29T23:00:00.000Z", "__v": 0, "died": "1745-10-18T22:00:00.000Z" }, "description": "A book about a big guy in a small world", "__v": 0, "stores": [ { "available": 8, "store": { "_id": "58a7a2529a8b894656a42dff", "name": "Book shop 1", "description": "This is where the cool kids buy all there books!", "address": "La La Land, 123 Combaja street", "__v": 0 }, "_id": "58a7a2529a8b894656a42e01" } ] } ]