如何修复“我有作者和书籍模式,在 Postman 结果中,我想显示一本书的两个或更多作者”
How to fix ' I have author & book schema, In Postman result, I want to show two or more author for a book'
我想要邮递员这样的结果
[{
title:
year:
rating:
authors: [{
name:
birthday:
country:
}]
}]
我想要两位或更多作者,但我只有一位作者
model/book.js
const mongoose = require('mongoose');
const authorSchema = require('../models/author');
const bookSchema = new mongoose.Schema({
title:{
type:String,
required:true,
min: 5,
max: 50
},
rating:{
type: Number,
required: true,
min:0,
max:10
},
authors: {
type: authorSchema,
required: true
},
});
const Book= new mongoose.model('Book', bookSchema);
route/book.js
router.get('/', async(req, res)=>{
const books= await Book
.find({}, { _id:0, __v:0 })
res.send(books);
});
router.post('/', async(req, res)=>{
const author = await Author.findById (req.body.authorId);
if(!author) return res.status(400).send('Invalid Author');
let book= new Book({
title: req.body.title,
rating: req.body.rating,
authors:[{
name: author.name,
birthday: author.birthday,
country: author.country
}]
});
book= await book.save();
res.send(book)
});
module.exports =router;
我在邮递员
中通过POST方法输入这个
{
"title": "Learn Python",
"rating": "9",
"authorId": [ "5d99ac95f17917117068631b",
“5d99ad75c4edd61f98af740b”]
}
然后我得到 Only first author, author array not show
findById 通过提供的 _id 字段查找单个文档
Author.find({ $in: req.body.authorId })
将为您提供一组与 req.body.authorId
.
匹配的作者
然后通过遍历 find
查询的结果来为您的 new Book
实例创建作者数组。
建议
仅使用 find
查询中需要的字段 - 例如 Author.find({ $in: req.body.authorId }, 'name birthday country')
、
此外,如果您有 Author
的另一个 collection,最好将参考文献 (_id) 保留在 Book
collection 而不是传递作者详细信息,这将消除您 collection 的数据冗余。
您可以在 mongodb here
中找到有关数据建模的更多信息
我想要邮递员这样的结果
[{
title:
year:
rating:
authors: [{
name:
birthday:
country:
}]
}]
我想要两位或更多作者,但我只有一位作者
model/book.js
const mongoose = require('mongoose');
const authorSchema = require('../models/author');
const bookSchema = new mongoose.Schema({
title:{
type:String,
required:true,
min: 5,
max: 50
},
rating:{
type: Number,
required: true,
min:0,
max:10
},
authors: {
type: authorSchema,
required: true
},
});
const Book= new mongoose.model('Book', bookSchema);
route/book.js
router.get('/', async(req, res)=>{
const books= await Book
.find({}, { _id:0, __v:0 })
res.send(books);
});
router.post('/', async(req, res)=>{
const author = await Author.findById (req.body.authorId);
if(!author) return res.status(400).send('Invalid Author');
let book= new Book({
title: req.body.title,
rating: req.body.rating,
authors:[{
name: author.name,
birthday: author.birthday,
country: author.country
}]
});
book= await book.save();
res.send(book)
});
module.exports =router;
我在邮递员
中通过POST方法输入这个
{
"title": "Learn Python",
"rating": "9",
"authorId": [ "5d99ac95f17917117068631b",
“5d99ad75c4edd61f98af740b”]
}
然后我得到 Only first author, author array not show
findById 通过提供的 _id 字段查找单个文档
Author.find({ $in: req.body.authorId })
将为您提供一组与 req.body.authorId
.
然后通过遍历 find
查询的结果来为您的 new Book
实例创建作者数组。
建议
仅使用 find
查询中需要的字段 - 例如 Author.find({ $in: req.body.authorId }, 'name birthday country')
、
此外,如果您有 Author
的另一个 collection,最好将参考文献 (_id) 保留在 Book
collection 而不是传递作者详细信息,这将消除您 collection 的数据冗余。
您可以在 mongodb here