Javascript 如何通过对象id访问其他属性值?
我几乎完成了我的OneToMany CRUD MEAN应用程序的构建,但我有一个问题 我的数据库中有两个集合 游戏:Javascript 如何通过对象id访问其他属性值?,javascript,angularjs,node.js,mongodb,mean-stack,Javascript,Angularjs,Node.js,Mongodb,Mean Stack,我几乎完成了我的OneToMany CRUD MEAN应用程序的构建,但我有一个问题 我的数据库中有两个集合 游戏: { "_id" : 1, "title" : "Prey", "developer" : "Arkane Studios", "publisherId" : 1, "year" : 2017, "gerne" : "Action" } 和出版商: { "_id" : 1, "name" : "Bethesda Softworks" } 在我的游戏详细信息html文
{ "_id" : 1,
"title" : "Prey",
"developer" : "Arkane Studios",
"publisherId" : 1,
"year" : 2017,
"gerne" : "Action" }
和出版商:
{ "_id" : 1,
"name" : "Bethesda Softworks" }
在我的游戏详细信息html文件中,我有以下内容:
<form>
<div class="form-group">
<label class="form-control">Title: {{ game.title }}</label>
<label class="form-control">Developer: {{ game.developer}}</label>
<label class="form-control">Publiser: {{ game.publisherId }}</label>
<label class="form-control">Year: {{ game.year }}</label>
<label class="form-control">Gerne: {{ game.gerne }}</label>
<label class="form-control">ID: {{ game._id }}</label>
<a href="#/games" class="btn btn-default"> Back</a>
标题:{{game.Title}
开发者:{{game.Developer}
发布者:{{game.publisherId}
年份:{{game.Year}
格恩:{{game.Gerne}
ID:{{game.\u ID}
有没有办法显示发行者名称而不是其id?您可以使用
$lookup
聚合函数在Mongoose
中与发行者进行左外连接游戏
收集,以获得嵌入游戏文档的发行者文档(如果存在)
您可以使用游戏中嵌入的发布者字段填充UI
出版者
> db.publishers.find({_id:1}).pretty()
{ "_id" : 1, "name" : "Bethesda Softworks" }
游戏
$lookup
> db.games.aggregate(
[
{
$lookup:
{
from : "publishers",
localField:"publisherId",
foreignField:"_id",
as : "publisher"
}
}
]
).pretty()
输出
{
"_id" : 1,
"title" : "Prey",
"developer" : "Arkane Studios",
"publisherId" : 1,
"year" : 2017,
"gerne" : "Action",
"publisher" : [
{
"_id" : 1,
"name" : "Bethesda Softworks"
}
]
}
>
你用猫鼬还是普通的猫鼬?我用猫鼬。你可以用猫鼬的。
{
"_id" : 1,
"title" : "Prey",
"developer" : "Arkane Studios",
"publisherId" : 1,
"year" : 2017,
"gerne" : "Action",
"publisher" : [
{
"_id" : 1,
"name" : "Bethesda Softworks"
}
]
}
>