我对猫鼬的“人口”方法有疑问。遵循以下结构
const CompanySchema = new Schema({
name: String,
logo: String,
description: String,
address: String,
website: String,
phone: String,
email: String,
warehouses: [{ type: Schema.Types.ObjectId, ref: 'CompanyWarehouses' }],
});
const CompanyWarehouses = new Schema({
company: { type: Schema.Types.ObjectId, ref: 'Company' },
city: String,
});
两个表中都有一些条目。我试图使用Population
中的Mongoose
,以便为每个公司填充warehouses
字段。
使用以下查询
Company.find({}, 'warehouses -_id')
.populate({
path: 'warehouses',
})
.exec((err, docs) => {
console.log(docs);
});
我得到以下结果
[ { warehouses: [ [Object], [Object], [Object] ] },
{ warehouses: [ [Object], [Object] ] },
{ warehouses: [ [Object], [Object] ] },
{ warehouses: [ [Object] ] },
{ warehouses: [] },
{ warehouses: [ [Object] ] } ]
如果我这样做的话,我会得到仓库的编号
[ { warehouses:
[ 5c2765e86351ec4f1e219d6b,
5c2765e86351ec4f1e219d6c,
5c2765e86351ec4f1e219d6d ] },
{ warehouses: [ 5c2773596351ec4f1e219d77, 5c2773596351ec4f1e219d78 ] },
{ warehouses: [ 5c277c766351ec4f1e219d82, 5c277c766351ec4f1e219d83 ] },
{ warehouses: [ 5c27843c6351ec4f1e219d90 ] },
{ warehouses: [] },
{ warehouses: [ 5c279b8ecebdea072f0bacd5 ] } ]
如果我查询CompanyWarehouses
的{{1}}和populate
,我会得到正确的人口
company
谁能解释一下我在这里想念什么
LE:
我已经定义了两个模型
CompanyWarehouses.find()
.populate('company')
.exec((err, docs) => {
console.log(docs);
});
[ { _id: 5c279b8ecebdea072f0bacd5,
company:
{ warehouses: [Array],
_id: 5c279b3acebdea072f0baccc,
name: 'sadsa',
phone: '2323',
email: 'sss@ss.ss',
city: 'dsadsadsa',
address: 'sadadsa',
logo: 'logo.png' },
city: 'ssdsa',
__v: 0 }]
答案 0 :(得分:3)
这是由于list = list.OrderByDescending(x => x.Item1).ToList();
限制了将显示的数据深度所致。
如果要查看console.log()
的全部内容,可以将其记录为JSON:
docs
或删除深度限制:
console.log('%j', docs);
答案 1 :(得分:0)
我认为问题出在这里
warehouses: [{ type: Schema.Types.ObjectId, ref: 'CompanyWarehouses' }]
您正在将CompanyWarehouses
的价值赋予ref
尝试同时创建两个架构常量的实例,然后将该实例变量分配给ref
const CompanySchema = new Schema({
...,
warehouses: [{ type: Schema.Types.ObjectId, ref: 'companywarehouses' }],
});
const CompanyWarehouses = new Schema({
company: { type: Schema.Types.ObjectId, ref: 'company' },
...
});
// instance of both Model
const CompanySch = mongoose.model('company', CompanySchema);
const WareHousesSch = mongoose.model('companywarehouses', CompanyWarehouses);