当foreignField是数组时,猫鼬无法填充虚拟

时间:2018-09-05 13:47:43

标签: mongoose mongoose-schema mongoose-populate

我正在尝试用猫鼬创建虚拟填充器,但是我对此感到困惑,我不知道这是一个限制,还是一个错误,或者仅仅是我做错了事。

这个想法是,用户可以是多个组的成员,然后我可以通过查询在组数组中具有其ID的用户来在组架构中填充虚拟。

我创建了一个示例(基于原始的猫鼬文档)。

卢卡·图里利(Luca Turilli)已经进入了多个乐队,有时是同时出现的,因此原始模型(该乐队是一个领域)并不适合意大利金属。

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('connected to db');
});

var artistSchema = new Schema ({
    name: String,
    bands: [{type: String, ref: 'band'}]
});

var bandSchema = new Schema ({
    name: String
},{
    toJson:{virtuals:true},
    toObject: {virtuals:true}
});
bandSchema.virtual('lineup', {
    ref: 'Artist',
    localField: 'name',
    foreignField: 'bands'
});

var Artist = mongoose.model('Artist', artistSchema);
var Band = mongoose.model('Band', bandSchema);
/* Comment this if you already populated the database */
Band.create({name:'Dreamquest'});
Band.create({name:'Luca Turilli'});
Band.create({name:'Rhapsody of Fire'});

Artist.create({name:'Luca Turilli', bands:['Dreamquest','Luca Turilli','Rhapsody of Fire']});
Artist.create({name:'Olaf Hayer', bands:['Luca Turilli']});
Artist.create({name:'Sascha Paeth', bands:['Luca Turilli','Dreamquest']});
Artist.create({name:'Robert Hunecke-Rizzo', bands:['Luca Turilli', 'Dreamquest']});
Artist.create({name:'Dominique Leurquin', bands:['Dreamquest', 'Rhapsody of Fire']});
Artist.create({name:'Patrice Guers', bands:['Rhapsody of Fire']});
Artist.create({name:'Alex Landenburg', bands:['Rhapsody of Fire']});
/*stop commenting here*/

Band.find(function(err, bands) {
    if (err) return console.error(err);
    console.log(bands);
});

预期输出将类似于:

[ { _id: 5b8fd9eef72e14315b52985f,
    name: 'Dreamquest',
    __v: 0,
    lineup: [Artist entries of Luca,Sascha,Robert and Dominique]},
  .... more bands
  ]

相反,我明白了

    [ { _id: 5b8fd9eef72e14315b52985f,
    name: 'Dreamquest',
    __v: 0,
    lineup: null,
    id: '5b8fd9eef72e14315b52985f' },
  { _id: 5b8fd9eef72e14315b529860,
    name: 'Luca Turilli',
    __v: 0,
    lineup: null,
    id: '5b8fd9eef72e14315b529860' },
  { _id: 5b8fd9eef72e14315b529861,
    name: 'Rhapsody of Fire',
    __v: 0,
    lineup: null,
    id: '5b8fd9eef72e14315b529861' } ]

我尝试搜索处于相同情况的示例或人物,但没有找到太多信息。我知道我可能可以使用 getter 函数来执行此操作,但是我想知道是否可以使用虚拟机执行此操作,或者我正在浪费时间。

1 个答案:

答案 0 :(得分:0)

最后,响应很简单:virtuals:true不会自动填充字段,并且虚拟对象不会自己填充。之所以令人困惑,是因为似乎填充虚拟表示虚拟填充本身...

无论如何正确的查询是:

Band.find({}).populate('lineup').exec(function(err,bands) {
    if (err) return console.error(err);
    console.log(bands);
}

与任何手动填充一样,如果您打算定期访问此信息,则可以进入“查找”,“查找”和“保存”(以及其他)设置为自动填充。