猫鼬中的选择和填充功能不起作用
我正在尝试获取包含电影标题的电影字段并填充类型集合。 但我得到的回应是具有所有属性的电影收藏。
const movies = await Movie.find()
.populate("genre", "name")
.sort("name")
.select("title");
我得到的回应
[ { "_id": "623724b0bdb7598002f04b83", “标题”:“泰坦尼克号”, “库存数量”:10, “每日租金”:5, “流派”:{ "_id": "61e923f6860764ef514a64b5", “名字”:“浪漫”, “__v”:0 } }, { "_id": "623724d6bdb7598002f04b85", “标题”:“哈利·波特”, “库存数量”:15, “每日租金”:5, “流派”:{ "_id": "61e923da860764ef514a64aa", “名称”:“动作”, “__v”:0 } } ]
I am trying to get the Movie field with movie title and populating the genre collection.
But the response I am getting is the movies collection with all properties.
const movies = await Movie.find()
.populate("genre", "name")
.sort("name")
.select("title");
Response I got
[
{
"_id": "623724b0bdb7598002f04b83",
"title": "Titanic",
"numberInStock": 10,
"dailyRentalRate": 5,
"genre": {
"_id": "61e923f6860764ef514a64b5",
"name": "romantic",
"__v": 0
}
},
{
"_id": "623724d6bdb7598002f04b85",
"title": "Harry Potter",
"numberInStock": 15,
"dailyRentalRate": 5,
"genre": {
"_id": "61e923da860764ef514a64aa",
"name": "action",
"__v": 0
}
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在选择字段前面使用 (-) 符号。
前任:
You can using (-) sign in front of the select field.
Ex:
您可以按以下方式更新查询
const movie = wait Movie.find()
.populate({ 路径: "名称",选择: "标题"})
you can update the query as following
const movies = await Movie.find()
.populate({ path: "name",select: "title"})