当 foreignField 是数组时,Mongoose 无法填充虚拟
Mongoose fails to populate virtual when foreignField is an Array
我正在尝试在 mongoose 中创建一个虚拟填充器,但我在这方面遇到了困难,我不知道这是一个限制、一个错误,还是我做错了什么。
我的想法是一个用户可以是多个组的成员,然后我可以通过查询在组数组中具有其 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 函数来执行此操作,但我想知道是否可以使用虚拟机来执行此操作,否则我在浪费时间。
最后,响应很简单:virtuals:true
不会自动填充字段,虚拟对象也不会自行填充。这令人困惑,因为 populated virtual 似乎意味着虚拟本身填充...
无论如何正确的查询是:
Band.find({}).populate('lineup').exec(function(err,bands) {
if (err) return console.error(err);
console.log(bands);
}
与任何手动填充一样,您可以连接到 'find'、'findone' 和 'save'(以及其他)以将其设置为自动填充,如果您打算访问此定期发布信息。
我正在尝试在 mongoose 中创建一个虚拟填充器,但我在这方面遇到了困难,我不知道这是一个限制、一个错误,还是我做错了什么。
我的想法是一个用户可以是多个组的成员,然后我可以通过查询在组数组中具有其 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 函数来执行此操作,但我想知道是否可以使用虚拟机来执行此操作,否则我在浪费时间。
最后,响应很简单:virtuals:true
不会自动填充字段,虚拟对象也不会自行填充。这令人困惑,因为 populated virtual 似乎意味着虚拟本身填充...
无论如何正确的查询是:
Band.find({}).populate('lineup').exec(function(err,bands) {
if (err) return console.error(err);
console.log(bands);
}
与任何手动填充一样,您可以连接到 'find'、'findone' 和 'save'(以及其他)以将其设置为自动填充,如果您打算访问此定期发布信息。