我想为MTG组合创建一个数据库,并使用Mongoose和MLab做一些非常简单的事情:我们有两个模式,Combo和Card,以及两个与Combo模型实例相关的Card模型实例。当我尝试使用这种组合的名称填充卡片的组合时,它不起作用,它仍然显示ID而不是名称。
var mongoose = require('mongoose') ;
mongoose.connect('mongodb://wernerbusch:xxxx!h@ds115340.mlab.com:15340/wernercito');
var Schema = mongoose.Schema;
var comboSchema = new Schema({
name : String,
cards : [{type: Schema.Types.ObjectId, ref:'Card'}],
type : ["infinite mana", "infinite damage", "draw library", "lethal damage", "storm", "graveyard"]
});
var cardSchema = new Schema({
name: String,
colour:["U","R","B","W","G","C"],
combos: [{type:Schema.Types.ObjectId, ref: 'Combo'}]
})
var Combo = mongoose.model("Combo",comboSchema);
var Card = mongoose.model("Card",cardSchema);
var PanderBurst = new Combo({
name:"PanderBurst",
type:'lethal damage'
})
PanderBurst.save(function(err){
if (err) console.log( err)
var SaprolingBurst = new Card({
name: "Saproling Burst",
colour:["G", "R"],
combos : PanderBurst._id
})
var Pandemonium = new Card({
name: "Pandemonium",
colour:["R"],
combos: PanderBurst._id
})
SaprolingBurst.save(function(err){
if (err) console.log( err)
});
Pandemonium.save(function(err){
if (err) console.log( err)
})
});
Card.find().populate('combos', 'name').exec(function(err,cards){
if (err) console.log (err);
})
执行此代码后,卡片文档中没有任何变化。
答案 0 :(得分:0)
你试过了吗?
<h2 class="uk-modal-title">ADD Blood Bank Member</h2>
<form method="post" action="" enctype="multipart/form-data">
<div class="uk-margin">
<input class="uk-input" type="text" placeholder="First Name" name="first" required>
</div>
<div class="uk-margin">
<input class="uk-input" type="text" placeholder="Last Name" name="last" required>
</div>
<div class="uk-margin">
<input class="uk-input" type="text" placeholder="Phone Number" name="phone" required>
</div>
<div class="uk-margin">
<input class="uk-input" type="text" placeholder="Age" name="age" required>
</div>
<div class="uk-margin">
Gender<select class="uk-select" name="gender" required>
<option selected>Male</option>
<option>Female</option>
</select>
</div>
使用对象一直对我有用。
更新: 你也有:
Card.find().populate({path: "comboes", select: "name"})
.exec((err,cards)=>{
if (err) console.log (err);
console.log(cards);
});
您的架构中的
并且在您的填充中comboes: {type:Schema.Types.ObjectId, ref: 'Combo'}
。
最后更新:
进行查询时使用Populate。在创建期间填充模式不是好习惯,因为它会导致重复,因此请调用每个查询填充。