我简化了下面的代码:
var buySchema = new mongoose.Schema({
userid:String,
timestamp:Number,
price:Number
});
var cardSchema = new mongoose.Schema({
cardid: String,
buy:[{type:mongoose.Schema.Types.ObjectId, ref:'buy'}],
});
var Card = mongoose.model('cards',cardSchema,'cards');
var Buy = mongoose.model('buy',buySchema,'buy');
Card.findOne({'cardid':cardid}, function(err, card) {
var newBuy = new Buy({'userid':userid, timestamp:null,price:null});
newBuy.save(function(err){
card.buy.addToSet(newBuy._id);
});
card.save(function(err,card){
console.log(card.buy); // card.buy[] has an ObjectId ref
});
});
Card.findOne({'cardid':cardid}, function(err, card) {
console.log(card.buy); // !!!!! card.buy[] does not have ObjectId ref !!!!!
});
buy[] 似乎也没有在 mongodb 中保存 ObjectId ref
如果没有 ObjectId refs,我以后应该如何填充卡片?他们去哪儿了?关于人口,我没有得到什么?