是否有办法删除猫鼬. schema . objectid当我从mongodb使用猫鼬获得一个项目? &



我有两个模型

Movies.js

import mongoose from 'mongoose';
const MovieSchema = new mongoose.Schema({
title: {
type: String,
required: [true, 'Please add a title to the movie'],
unique: true,
trim: true
},
description: {
type: String,
required: [true, 'Please add a description']
},
video: {
type: String,
required: [true, 'Please add a video']
},
genre: {
type: [String],
required: true,
enum: [
'Drama',
'Action',
'Comedy',
'Documentary',
]
}
}, {
toJSON: { virtuals: true },
toObject: { virtuals: true }
})
MovieSchema.virtual('actors', {
ref: 'Actor',
localField: '_id',
foreignField: 'movies.movie',
justOne: false
})

const Movie = mongoose.model('Movie', MovieSchema);
export default Movie;

和Actor.js

import mongoose from 'mongoose'
const ActorSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: [true, 'Please add an actor name']
},
born: {
type: String,
required: [true, 'Please add a born date']
},
bornCountry: {
type: String,
required: [true, 'Please add a country']
},
description: {
type: String,
required: [true, 'Please add an actor description']
},
movies: [{
_id: false,
movie: {
type: mongoose.Schema.ObjectId,
ref: 'Movie'
},
role: {
type: String
}
}]
})

const Actor = mongoose.model('Actor', ActorSchema);
export default Actor;

这是我的控制器方法,从db

获取单个电影
const getMovie = async (req, res, next) => {
const movie = await Movie.findById(req.params.id).populate('actors', 'name')
if (!movie) {
res.status(201).json({ success: false, message: 'Movie not found' })
}
res.status(200).json({ success: true, data: movie })
}

这就是响应

{
"success": true,
"data": {
"genre": [
"Action"
],
"_id": "603fca3353ffd238484c6fab",
"title": "Gambito da rainha",
"description": "Orphaned at the tender age of nine, prodigious introvert Beth Harmon discovers and masters the game of chess in 1960s USA. But child stardom comes at a price.",
"video": "https://www.youtube.com/watch?v=CDrieqwSdgI",
"__v": 0,
"actors": [
{
"_id": "60400f2e9c782f3d600a93f5",
"name": "Anya Taylor-Joy",
"movies": [
{
"movie": "603fca3353ffd238484c6fab"
}
]
}
],
"id": "603fca3353ffd238484c6fab"
}

}

有没有办法不从响应中发送回data.actors.movies.movie id ?我试图只选择与。populate('actors', 'name')的名称,但我仍然得到mongoose.Schema.ObjectId在我的响应。

通过虚拟模式引用的字段仅在执行查询后计算,因此在填充期间不可访问,请参阅限制。如果你想取消该属性的设置,你应该使用一个自定义函数来排除它。

另一个解决方案是更改您的Movie模式,包括Actor引用:

const MovieSchema = new mongoose.Schema({
title: {
type: String,
required: [true, 'Please add a title to the movie'],
unique: true,
trim: true
},
description: {
type: String,
required: [true, 'Please add a description']
},
video: {
type: String,
required: [true, 'Please add a video']
},
genre: {
type: [String],
required: true,
enum: [
'Drama',
'Action',
'Comedy',
'Documentary',
]
},
actors: [{ type: mongoose.Schema.ObjectId, ref: 'Actor' }]
}, {
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
const Movie = mongoose.model('Movie', MovieSchema);

查询:

const movie = await Movie.findById(req.params.id).populate({
path: 'actors', 
select: 'name born bornCountry description'
});

结果预期:

{
genre: [ 'Action' ],
actors: [
{
_id: 6040d7c9965dd306b41c7e9e,
name: 'Anya Taylor-Joy',
born: 'England',
bornCountry: 'England',
description: 'actor'
}
],
_id: 6040d7c9965dd306b41c7e9d,
title: 'Gambito da Rainha',
description: 'Orphaned at the tender age of nine',
video: 'https://www.youtube.com/watch?v=CDrieqwSdgI',
__v: 1,
id: '6040d7c9965dd306b41c7e9d'
}

最新更新