数组中 ObjectId 的 $lookup 不起作用
$lookup on ObjectId's in an array does not work
我遇到了与这个问题相同的情况:(我无法对这个问题发表评论,因为我没有足够的声誉......抱歉重复......) - 但是
该查询对我不起作用,我在它的末尾得到一个空数组。
我想获取一份公司列表及其职位字段,但职位字段始终为空
在下面的代码中。 (我得到了公司字段,并且有了 $unwind - 即使是公司字段也没有返回)。
我做错了什么?
猫鼬版本:5.7.14
mongodb版本:4.2.3
我也尝试了很多聚合和填充的组合,其中 none 成功了。
const schema = new Schema(
{
email: {
type: String,
required: true,
unique: true
},
name: {
type: String,
required: true,
unique: true
},
positionsIds: [{
type: Schema.Types.ObjectId,
ref: 'Position',
require: false
}
);
module.exports = mongoose.model('Company', schema);
-----------------------------
const schema = new Schema(
{
title: {
type: String,
required: true
},
requirements: [{
years: { type: Number, required: false },
skill: { type: String, required: false },
}],
companyId: {
type: Schema.Types.ObjectId,
ref: 'Company',
required: true
},
}
);
module.exports = mongoose.model('Position', schema );
---------------------------
let companies = await Company.aggregate([
{
$lookup:
{
from: 'Position',
localField: '_id',
foreignField: 'companyId',
as: 'positions'
}
},
// {
// $unwind: '$positions' // - mess the query even more and return an empty array
// },
]);
Mongoose 根据约定创建 MongoDB 集合。 Position
由于型号名称 translated 变为复数形式 positions
因此您的查询应如下所示:
{
$lookup: {
from: 'positions',
localField: '_id',
foreignField: 'companyId',
as: 'positions'
}
}
The first argument is the singular name of the collection your model is for. ** Mongoose automatically looks for the plural, lowercased version of your model name. ** Thus, for the example above, the model Tank is for the tanks collection in the database.
我遇到了与这个问题相同的情况:
我想获取一份公司列表及其职位字段,但职位字段始终为空 在下面的代码中。 (我得到了公司字段,并且有了 $unwind - 即使是公司字段也没有返回)。
我做错了什么?
猫鼬版本:5.7.14 mongodb版本:4.2.3
我也尝试了很多聚合和填充的组合,其中 none 成功了。
const schema = new Schema(
{
email: {
type: String,
required: true,
unique: true
},
name: {
type: String,
required: true,
unique: true
},
positionsIds: [{
type: Schema.Types.ObjectId,
ref: 'Position',
require: false
}
);
module.exports = mongoose.model('Company', schema);
-----------------------------
const schema = new Schema(
{
title: {
type: String,
required: true
},
requirements: [{
years: { type: Number, required: false },
skill: { type: String, required: false },
}],
companyId: {
type: Schema.Types.ObjectId,
ref: 'Company',
required: true
},
}
);
module.exports = mongoose.model('Position', schema );
---------------------------
let companies = await Company.aggregate([
{
$lookup:
{
from: 'Position',
localField: '_id',
foreignField: 'companyId',
as: 'positions'
}
},
// {
// $unwind: '$positions' // - mess the query even more and return an empty array
// },
]);
Mongoose 根据约定创建 MongoDB 集合。 Position
由于型号名称 translated 变为复数形式 positions
因此您的查询应如下所示:
{
$lookup: {
from: 'positions',
localField: '_id',
foreignField: 'companyId',
as: 'positions'
}
}
The first argument is the singular name of the collection your model is for. ** Mongoose automatically looks for the plural, lowercased version of your model name. ** Thus, for the example above, the model Tank is for the tanks collection in the database.