猫鼬填充数组中的单个项目

Posted

技术标签:

【中文标题】猫鼬填充数组中的单个项目【英文标题】:Mongoose populate single item in array 【发布时间】:2019-04-06 02:08:23 【问题描述】:

我有一个包含动态引用数组的模型。

var postSchema = new Schema(
  name: String,
  targets: [
    kind: String,
    item:  type: ObjectId, refPath: 'targets.kind' 
  ]
); 

我正在使用 targets 属性来存储对多个不同模型、用户、线程、附件等的引用。

是否可以只填充我想要的引用?

Post.find().populate(
  // Does not work
  // match:  'targets.kind': 'Thread' , // I want to populate only the references that match. ex: Thread, User, Attachment
  path: 'targets.item',
  model: 'targets.kind',
  select: '_id title',
);

谢谢

【问题讨论】:

【参考方案1】:

这里的一个重要教训应该是mongoose.set('debug', true) 是你的新“最好的朋友”。这将显示您正在编写的代码向 MongoDB 发出的实际查询,这非常重要,因为当您真正“看到”它时,它会清除您可能有的任何误解。

逻辑问题

让我们演示一下为什么您正在尝试失败:

const  Schema  = mongoose = require('mongoose');

const uri = 'mongodb://localhost:27017/polypop';

mongoose.set('debug', true);
mongoose.Promise = global.Promise;

const postSchema = new Schema(
  name: String,
  targets: [
    kind: String,
    item:  type: Schema.Types.ObjectId, refPath: 'targets.kind' 
  ]
);

const fooSchema = new Schema(
 name: String
)

const barSchema = new Schema(
  number: Number
);

const Post = mongoose.model('Post', postSchema);
const Foo = mongoose.model('Foo', fooSchema);
const Bar = mongoose.model('Bar', barSchema);

const log = data => console.log(JSON.stringify(data, undefined, 2));

(async function() 

  try 

    const conn = await mongoose.connect(uri,  useNewUrlParser: true );

    // Clean all data
    await Promise.all(
      Object.entries(conn.models).map(([k,m]) => m.deleteMany())
    );

    // Create some things
    let [foo, bar] = await Promise.all(
      [ _t: 'Foo', name: 'Bill' ,  _t: 'Bar', number: 1 ]
        .map(( _t, ...d ) => mongoose.model(_t).create(d))
    );

    log([foo, bar]);

    // Add a Post

    let post = await Post.create(
      name: 'My Post',
      targets: [ kind: 'Foo', item: foo ,  kind: 'Bar', item: bar ]
    );

    log(post);

    let found = await Post.findOne();
    log(found);

    let result = await Post.findOne()
      .populate(
        match:  'targets.kind': 'Foo' ,    // here is the problem!
        path: 'targets.item',
      );

    log(result);

   catch(e) 
    console.error(e);
   finally 
    mongoose.disconnect();
  

)()

所以那里的评论显示match是逻辑问题,所以让我们看看调试输出,看看为什么:

Mongoose: posts.deleteMany(, )
Mongoose: foos.deleteMany(, )
Mongoose: bars.deleteMany(, )
Mongoose: foos.insertOne( _id: ObjectId("5bdbc70996ed8e3295b384a0"), name: 'Bill', __v: 0 )
Mongoose: bars.insertOne( _id: ObjectId("5bdbc70996ed8e3295b384a1"), number: 1, __v: 0 )
[
  
    "_id": "5bdbc70996ed8e3295b384a0",
    "name": "Bill",
    "__v": 0
  ,
  
    "_id": "5bdbc70996ed8e3295b384a1",
    "number": 1,
    "__v": 0
  
]
Mongoose: posts.insertOne( _id: ObjectId("5bdbc70996ed8e3295b384a2"), name: 'My Post', targets: [  _id: ObjectId("5bdbc70996ed8e3295b384a4"), kind: 'Foo', item: ObjectId("5bdbc70996ed8e3295b384a0") ,  _id: ObjectId("5bdbc70996ed8e3295b384a3"), kind: 'Bar', item: ObjectId("5bdbc70996ed8e3295b384a1")  ], __v: 0 )

  "_id": "5bdbc70996ed8e3295b384a2",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbc70996ed8e3295b384a4",
      "kind": "Foo",
      "item": 
        "_id": "5bdbc70996ed8e3295b384a0",
        "name": "Bill",
        "__v": 0
      
    ,
    
      "_id": "5bdbc70996ed8e3295b384a3",
      "kind": "Bar",
      "item": 
        "_id": "5bdbc70996ed8e3295b384a1",
        "number": 1,
        "__v": 0
      
    
  ],
  "__v": 0

Mongoose: posts.findOne(,  projection:  )

  "_id": "5bdbc70996ed8e3295b384a2",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbc70996ed8e3295b384a4",
      "kind": "Foo",
      "item": "5bdbc70996ed8e3295b384a0"
    ,
    
      "_id": "5bdbc70996ed8e3295b384a3",
      "kind": "Bar",
      "item": "5bdbc70996ed8e3295b384a1"
    
  ],
  "__v": 0

Mongoose: posts.findOne(,  projection:  )
Mongoose: bars.find( 'targets.kind': 'Foo', _id:  '$in': [ ObjectId("5bdbc70996ed8e3295b384a1") ]  ,  projection:  )
Mongoose: foos.find( 'targets.kind': 'Foo', _id:  '$in': [ ObjectId("5bdbc70996ed8e3295b384a0") ]  ,  projection:  )

  "_id": "5bdbc70996ed8e3295b384a2",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbc70996ed8e3295b384a4",
      "kind": "Foo",
      "item": null
    ,
    
      "_id": "5bdbc70996ed8e3295b384a3",
      "kind": "Bar",
      "item": null
    
  ],
  "__v": 0

这是显示其他一切都在实际工作的完整输出,事实上,如果没有match,您将获得项目的填充数据。但请仔细查看向foobar 集合发出的两个查询:

Mongoose: bars.find( 'targets.kind': 'Foo', _id:  '$in': [ ObjectId("5bdbc70996ed8e3295b384a1") ]  ,  projection:  )
Mongoose: foos.find( 'targets.kind': 'Foo', _id:  '$in': [ ObjectId("5bdbc70996ed8e3295b384a0") ]  ,  projection:  )

因此,您在match 下包含的'targets.kind' 实际上是在foobar 集合中搜索的,而在posts 集合中不是期待。除了输出的其余部分,这应该让您了解 populate() 的实际工作原理,因为 nothing 曾经说过专门返回 kind: 'Foo' 的“数组条目”为示例如下。

这个“过滤数组”的过程实际上甚至不是一个自然的 MongoDB 查询,除了“第一个和 singular 匹配”之外,您实际上通常会使用 @987654344 @ 和 $filter 运算符。您可以通过位置 $ 运算符获得“单数”,但如果您想要“所有 foos”,其中有多个,那么它需要 $filter

所以这里真正的核心问题是populate()实际上是“过滤数组”的错误位置和错误操作。相反,你真的想“聪明地”只返回你想要的数组条目之前你去做任何其他事情来“填充”项目。

结构问题

从上面的列表中注意到,这是问题中暗示的寓言,为了“加入”并获得整体结果,引用了“多个模型”。虽然这在“RDBMS 领域”中似乎是合乎逻辑的,但对于 MongoDB 和“文档数据库”的一般“同类”而言,这样做肯定不可行,也不实际或有效。

这里要记住的关键是“集合”中的“文档”不必都具有与 RDBMS 相同的“表结构”。结构可以变化,虽然最好不要“变化很大”,但将“多态对象”存储在单个集合中肯定是非常有效的。毕竟,您实际上希望将所有这些东西都引用回同一个父级,那么为什么它们需要位于不同的集合中呢?简单地说,它们根本不需要:

const  Schema  = mongoose = require('mongoose');

const uri = 'mongodb://localhost:27017/polypop';

mongoose.set('debug', true);
mongoose.Promise = global.Promise;

const postSchema = new Schema(
  name: String,
  targets: [
    kind: String,
    item:  type: Schema.Types.ObjectId, ref: 'Target' 
  ]
);

const targetSchema = new Schema();

const fooSchema = new Schema(
  name: String
);

const barSchema = new Schema(
  number: Number
);

const bazSchema = new Schema(
  title: String
);

const log = data => console.log(JSON.stringify(data, undefined, 2));

const Post = mongoose.model('Post', postSchema);
const Target = mongoose.model('Target', targetSchema);
const Foo = Target.discriminator('Foo', fooSchema);
const Bar = Target.discriminator('Bar', barSchema);
const Baz = Target.discriminator('Baz', bazSchema);

(async function() 

  try 

    const conn = await mongoose.connect(uri, useNewUrlParser: true );

    // Clean data - bit hacky but just a demo
    await Promise.all(
      Object.entries(conn.models).map(([k, m]) => m.deleteMany() )
    );

    // Insert some things
    let [foo1, bar, baz, foo2] = await Promise.all(
      [
         _t: 'Foo', name: 'Bill' ,
         _t: 'Bar', number: 1 ,
         _t: 'Baz', title: 'Title' ,
         _t: 'Foo', name: 'Ted' 
      ].map(( _t, ...d ) => mongoose.model(_t).create(d))
    );

    log([foo1, bar, baz, foo2]);

    // Add a Post
    let post = await Post.create(
      name: 'My Post',
      targets: [
         kind: 'Foo', item: foo1 ,
         kind: 'Bar', item: bar ,
         kind: 'Baz', item: baz ,
         kind: 'Foo', item: foo2 
      ]
    );

    log(post);

    let found = await Post.findOne();
    log(found);

    let result1 = await Post.findOne()
      .populate(
        path: 'targets.item',
        match:  __t: 'Foo' 
      );
    log(result1);

    let result2 = await Post.aggregate([
      // Only get documents with a matching entry
       "$match": 
        "targets.kind": "Foo"
      ,
      // Optionally filter the array
       "$addFields": 
        "targets": 
          "$filter": 
            "input": "$targets",
            "cond": 
              "$eq": [ "$$this.kind", "Foo" ]
             
          
        
      ,
      // Lookup from single source
       "$lookup": 
        "from": Target.collection.name,
        "localField": "targets.item",
        "foreignField": "_id",
        "as": "matches"
      ,
      // Marry up arrays
       "$project": 
        "name": 1,
        "targets": 
          "$map": 
            "input": "$targets",
            "in": 
              "kind": "$$this.kind",
              "item": 
                "$arrayElemAt": [
                  "$matches",
                   "$indexOfArray": [ "$matches._id", "$$this.item" ] 
                ]
              
            
          
        
      
    ]);
    log(result2);

    let result3 = await Post.aggregate([
      // Only get documents with a matching entry
       "$match": 
        "targets.kind": "Foo"
      ,
      // Optionally filter the array
       "$addFields": 
        "targets": 
          "$filter": 
            "input": "$targets",
            "cond": 
              "$eq": [ "$$this.kind", "Foo" ]
             
          
        
      ,
      // Lookup from single source with overkill of type check
       "$lookup": 
        "from": Target.collection.name,
        "let":  "targets": "$targets" ,
        "pipeline": [
           "$match": 
            "$expr": 
              "$in": [ "$_id", "$$targets.item" ]
            ,
            "__t": "Foo"
          
        ],
        "as": "matches"
      ,
      // Marry up arrays
       "$project": 
        "name": 1,
        "targets": 
          "$map": 
            "input": "$targets",
            "in": 
              "kind": "$$this.kind",
              "item": 
                "$arrayElemAt": [
                  "$matches",
                   "$indexOfArray": [ "$matches._id", "$$this.item" ] 
                ]
              
            
          
        
      
    ]);
    console.log(result3);    

   catch(e) 
    console.error(e);
   finally 
    mongoose.disconnect();
  

)()

这有点长,还有更多概念要解决,但基本原则是我们只使用一个,而不是对不同类型使用“多个集合”。 “mongoose”方法在模型设置中使用"discriminators",这与这部分代码有关:

const Post = mongoose.model('Post', postSchema);
const Target = mongoose.model('Target', targetSchema);
const Foo = Target.discriminator('Foo', fooSchema);
const Bar = Target.discriminator('Bar', barSchema);
const Baz = Target.discriminator('Baz', bazSchema);

这实际上只是从“单一”集合的“基本模型”调用.discriminator(),而不是调用mongoose.model()。这样做的真正好处是就您的其余代码而言,BazBar 等都被透明地视为“模型”,但它们实际上在做一些非常酷的事情。

所以所有这些“相关的东西”(它们确实是,即使你还不这么认为)实际上都保存在同一个集合中,但是使用单个模型的操作会考虑到“自动”kind钥匙。默认情况下这是__t,但您实际上可以在选项中指定您想要的任何内容。

这些实际上都在同一个集合中这一事实非常重要,因为您基本上可以轻松地查询同一个集合以获取不同类型的数据。简单地说:

Foo.find()

真的会打电话

targets.find( __t: 'Foo' )

并自动执行此操作。但更重要的是

Target.find( __t:  "$in": [ 'Foo', 'Baz' ]  )

将通过“单个请求”从“单个集合”返回所有预期结果。

那么看看这个结构下修改后的populate()

let result1 = await Post.findOne()
  .populate(
    path: 'targets.item',
    match:  __t: 'Foo' 
  );
log(result1);

这会显示在日志中:

Mongoose: posts.findOne(,  projection:  )
Mongoose: targets.find( __t: 'Foo', _id:  '$in': [ ObjectId("5bdbe2895b1b843fba050569"), ObjectId("5bdbe2895b1b843fba05056a"), ObjectId("5bdbe2895b1b843fba05056b"), ObjectId("5bdbe2895b1b843fba05056c") ]  ,  projection:  )

请注意,即使所有“四个”相关的 ObjectId 值都随请求一起发送,__t: 'Foo' 的附加约束也绑定了实际返回和合并的文档。然后结果变得不言而喻,因为仅填充了 'Foo' 条目。但也要注意“catch”:


  "_id": "5bdbe2895b1b843fba05056d",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbe2895b1b843fba050571",
      "kind": "Foo",
      "item": 
        "__t": "Foo",
        "_id": "5bdbe2895b1b843fba050569",
        "name": "Bill",
        "__v": 0
      
    ,
    
      "_id": "5bdbe2895b1b843fba050570",
      "kind": "Bar",
      "item": null
    ,
    
      "_id": "5bdbe2895b1b843fba05056f",
      "kind": "Baz",
      "item": null
    ,
    
      "_id": "5bdbe2895b1b843fba05056e",
      "kind": "Foo",
      "item": 
        "__t": "Foo",
        "_id": "5bdbe2895b1b843fba05056c",
        "name": "Ted",
        "__v": 0
      
    
  ],
  "__v": 0

填充后过滤

这实际上是一个更长的主题和更多的fully answered elsewhere,但这里的基本原理如上面的输出所示是populate() 实际上仍然完全没有对数组中的结果进行实际“过滤”以仅显示所需的匹配项.

另一件事是,从“性能”的角度来看,populate() 确实不是一个好主意,因为真正发生的是“另一个查询”(在我们的第二种形式中,我们只优化了一个)或者可能取决于您的结构的“许多查询”实际上正在发送到数据库,并且结果正在客户端上一起重建。

总体而言,您最终返回的数据比实际需要的多得多,而且您最多只能依靠手动客户端过滤来丢弃那些不需要的结果。因此,“理想”的位置是让“服务器”代替执行此类操作,并且返回您实际需要的数据。

很久以前,populate() 方法作为“方便”添加到 mongoose API 中。从那时起,MongoDB 继续前进,现在将 $lookup 作为一种“原生”方式,通过单个请求在服务器上执行“加入”。

有不同的方法可以做到这一点,但仅涉及与现有 populate() 功能密切相关但有改进的“两个”:

let result2 = await Post.aggregate([
  // Only get documents with a matching entry
   "$match": 
    "targets.kind": "Foo"
  ,
  // Optionally filter the array
   "$addFields": 
    "targets": 
      "$filter": 
        "input": "$targets",
        "cond": 
          "$eq": [ "$$this.kind", "Foo" ]
         
      
    
  ,
  // Lookup from single source
   "$lookup": 
    "from": Target.collection.name,
    "localField": "targets.item",
    "foreignField": "_id",
    "as": "matches"
  ,
  // Marry up arrays
   "$project": 
    "name": 1,
    "targets": 
      "$map": 
        "input": "$targets",
        "in": 
          "kind": "$$this.kind",
          "item": 
            "$arrayElemAt": [
              "$matches",
               "$indexOfArray": [ "$matches._id", "$$this.item" ] 
            ]
          
        
      
    
  
]);
log(result2);

其中的两个基本“优化”使用$filter 来“预先丢弃”数组中实际上与我们想要的类型不匹配的项目。这可以是完全可选的,稍后会详细介绍,但在可能的情况下,这可能是一件好事,因为我们甚至不会在外部集合中寻找匹配的 _id 值,而不是 'Foo' 的东西.

另一个当然是$lookup 本身,这意味着我们实际上只是创建一个,而不是单独往返服务器,并且在返回任何响应之前完成“加入”。在这里,我们只在外部集合中寻找与target.items 数组条目值匹配的_id 值。我们已经为'Foo' 过滤了这些内容,所以这就是返回的全部内容:

  
    "_id": "5bdbe6aa2c4a2240c16802e2",
    "name": "My Post",
    "targets": [
      
        "kind": "Foo",
        "item": 
          "_id": "5bdbe6aa2c4a2240c16802de",
          "__t": "Foo",
          "name": "Bill",
          "__v": 0
        
      ,
      
        "kind": "Foo",
        "item": 
          "_id": "5bdbe6aa2c4a2240c16802e1",
          "__t": "Foo",
          "name": "Ted",
          "__v": 0
        
      
    ]
  

对于“轻微”的变化,我们实际上甚至可以使用 MongoDB 3.6 及更高版本的“子管道”处理来检查 $lookup 表达式中的 __t 值。这里的主要用例是,如果您选择完全从父 Post 中删除 kind 并简单地依赖存储中使用的鉴别器引用所固有的“种类”信息:

let result3 = await Post.aggregate([
  // Only get documnents with a matching entry
   "$match": 
    "targets.kind": "Foo"
  ,
  // Optionally filter the array
   "$addFields": 
    "targets": 
      "$filter": 
        "input": "$targets",
        "cond": 
          "$eq": [ "$$this.kind", "Foo" ]
         
      
    
  ,
  // Lookup from single source with overkill of type check
   "$lookup": 
    "from": Target.collection.name,
    "let":  "targets": "$targets" ,
    "pipeline": [
       "$match": 
        "$expr": 
          "$in": [ "$_id", "$$targets.item" ]
        ,
        "__t": "Foo"
      
    ],
    "as": "matches"
  ,
  // Marry up arrays
   "$project": 
    "name": 1,
    "targets": 
      "$map": 
        "input": "$targets",
        "in": 
          "kind": "$$this.kind",
          "item": 
            "$arrayElemAt": [
              "$matches",
               "$indexOfArray": [ "$matches._id", "$$this.item" ] 
            ]
          
        
      
    
  
]);
log(result3);

这具有相同的“过滤”结果,同样是“单一请求”和“单一响应”。

整个主题变得更广泛了,尽管聚合管道可能看起来比简单的 populate() 调用更笨拙,但编写一个可以从模型中抽象出来并几乎生成大部分数据的包装器是相当简单的需要结构代码。您可以在"Querying after populate in Mongoose" 上查看此操作的概述,本质上与您在此处提出的问题基本相同,一旦我们理清了“多个集合连接”的初始问题以及为什么您真的不需要它们。

这里需要注意的是$lookup 实际上无法“动态”确定要“加入”到哪个集合。您需要像此处所做的那样静态地包含该信息,因此这是实际支持“鉴别器”而不是使用多个集合的另一个原因。这不仅是“更好的性能”,而且实际上是性能最高的选项真正支持您尝试做的事情的唯一方式。


作为参考,第二个清单的“完整”(由于最大帖子长度而被截断)输出将是:

Mongoose: posts.deleteMany(, )
Mongoose: targets.deleteMany(, )
Mongoose: targets.deleteMany(, )
Mongoose: targets.deleteMany(, )
Mongoose: targets.deleteMany(, )
Mongoose: targets.insertOne( _id: ObjectId("5bdbe2895b1b843fba050569"), __t: 'Foo', name: 'Bill', __v: 0 )
Mongoose: targets.insertOne( _id: ObjectId("5bdbe2895b1b843fba05056a"), __t: 'Bar', number: 1, __v: 0 )
Mongoose: targets.insertOne( _id: ObjectId("5bdbe2895b1b843fba05056b"), __t: 'Baz', title: 'Title', __v: 0 )
Mongoose: targets.insertOne( _id: ObjectId("5bdbe2895b1b843fba05056c"), __t: 'Foo', name: 'Ted', __v: 0 )
[
  
    "_id": "5bdbe2895b1b843fba050569",
    "__t": "Foo",
    "name": "Bill",
    "__v": 0
  ,
  
    "_id": "5bdbe2895b1b843fba05056a",
    "__t": "Bar",
    "number": 1,
    "__v": 0
  ,
  
    "_id": "5bdbe2895b1b843fba05056b",
    "__t": "Baz",
    "title": "Title",
    "__v": 0
  ,
  
    "_id": "5bdbe2895b1b843fba05056c",
    "__t": "Foo",
    "name": "Ted",
    "__v": 0
  
]
Mongoose: posts.insertOne( _id: ObjectId("5bdbe2895b1b843fba05056d"), name: 'My Post', targets: [  _id: ObjectId("5bdbe2895b1b843fba050571"), kind: 'Foo', item: ObjectId("5bdbe2895b1b843fba050569") ,  _id: ObjectId("5bdbe2895b1b843fba050570"), kind: 'Bar', item: ObjectId("5bdbe2895b1b843fba05056a") ,  _id: ObjectId("5bdbe2895b1b843fba05056f"), kind: 'Baz', item: ObjectId("5bdbe2895b1b843fba05056b") ,  _id: ObjectId("5bdbe2895b1b843fba05056e"), kind: 'Foo', item: ObjectId("5bdbe2895b1b843fba05056c")  ], __v: 0 )

  "_id": "5bdbe2895b1b843fba05056d",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbe2895b1b843fba050571",
      "kind": "Foo",
      "item": 
        "_id": "5bdbe2895b1b843fba050569",
        "__t": "Foo",
        "name": "Bill",
        "__v": 0
      
    ,
    
      "_id": "5bdbe2895b1b843fba050570",
      "kind": "Bar",
      "item": 
        "_id": "5bdbe2895b1b843fba05056a",
        "__t": "Bar",
        "number": 1,
        "__v": 0
      
    ,
    
      "_id": "5bdbe2895b1b843fba05056f",
      "kind": "Baz",
      "item": 
        "_id": "5bdbe2895b1b843fba05056b",
        "__t": "Baz",
        "title": "Title",
        "__v": 0
      
    ,
    
      "_id": "5bdbe2895b1b843fba05056e",
      "kind": "Foo",
      "item": 
        "_id": "5bdbe2895b1b843fba05056c",
        "__t": "Foo",
        "name": "Ted",
        "__v": 0
      
    
  ],
  "__v": 0

Mongoose: posts.findOne(,  projection:  )

  "_id": "5bdbe2895b1b843fba05056d",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbe2895b1b843fba050571",
      "kind": "Foo",
      "item": "5bdbe2895b1b843fba050569"
    ,
    
      "_id": "5bdbe2895b1b843fba050570",
      "kind": "Bar",
      "item": "5bdbe2895b1b843fba05056a"
    ,
    
      "_id": "5bdbe2895b1b843fba05056f",
      "kind": "Baz",
      "item": "5bdbe2895b1b843fba05056b"
    ,
    
      "_id": "5bdbe2895b1b843fba05056e",
      "kind": "Foo",
      "item": "5bdbe2895b1b843fba05056c"
    
  ],
  "__v": 0

Mongoose: posts.findOne(,  projection:  )
Mongoose: targets.find( __t: 'Foo', _id:  '$in': [ ObjectId("5bdbe2895b1b843fba050569"), ObjectId("5bdbe2895b1b843fba05056a"), ObjectId("5bdbe2895b1b843fba05056b"), ObjectId("5bdbe2895b1b843fba05056c") ]  ,  projection:  )

  "_id": "5bdbe2895b1b843fba05056d",
  "name": "My Post",
  "targets": [
    
      "_id": "5bdbe2895b1b843fba050571",
      "kind": "Foo",
      "item": 
        "__t": "Foo",
        "_id": "5bdbe2895b1b843fba050569",
        "name": "Bill",
        "__v": 0
      
    ,
    
      "_id": "5bdbe2895b1b843fba050570",
      "kind": "Bar",
      "item": null
    ,
    
      "_id": "5bdbe2895b1b843fba05056f",
      "kind": "Baz",
      "item": null
    ,
    
      "_id": "5bdbe2895b1b843fba05056e",
      "kind": "Foo",
      "item": 
        "__t": "Foo",
        "_id": "5bdbe2895b1b843fba05056c",
        "name": "Ted",
        "__v": 0
      
    
  ],
  "__v": 0

Mongoose: posts.aggregate([  '$match':  'targets.kind': 'Foo'  ,  '$addFields':  targets:  '$filter':  input: '$targets', cond:  '$eq': [ '$$this.kind', 'Foo' ]     ,  '$lookup':  from: 'targets', localField: 'targets.item', foreignField: '_id', as: 'matches'  ,  '$project':  name: 1, targets:  '$map':  input: '$targets', in:  kind: '$$this.kind', item:  '$arrayElemAt': [ '$matches',  '$indexOfArray': [ '$matches._id', '$$this.item' ]  ]       ], )
[
  
    "_id": "5bdbe2895b1b843fba05056d",
    "name": "My Post",
    "targets": [
      
        "kind": "Foo",
        "item": 
          "_id": "5bdbe2895b1b843fba050569",
          "__t": "Foo",
          "name": "Bill",
          "__v": 0
        
      ,
      
        "kind": "Foo",
        "item": 
          "_id": "5bdbe2895b1b843fba05056c",
          "__t": "Foo",
          "name": "Ted",
          "__v": 0
        
      
    ]
  
]
Mongoose: posts.aggregate([  '$match':  'targets.kind': 'Foo'  ,  '$addFields':  targets:  '$filter':  input: '$targets', cond:  '$eq': [ '$$this.kind', 'Foo' ]     ,  '$lookup':  from: 'targets', let:  targets: '$targets' , pipeline: [  '$match':  '$expr':  '$in': [ '$_id', '$$targets.item' ] , __t: 'Foo'   ], as: 'matches'  ,  '$project':  name: 1, targets:  '$map':  input: '$targets', in:  kind: '$$this.kind', item:  '$arrayElemAt': [ '$matches',  '$indexOfArray': [ '$matches._id', '$$this.item' ]  ]       ], )

【讨论】:

以上是关于猫鼬填充数组中的单个项目的主要内容,如果未能解决你的问题,请参考以下文章

如何在子文档中填充模型实例数组? MongoDB猫鼬

从猫鼬中的数组对象中选择特定项目

从猫鼬聚合管道中的数组数组中获取单个数组

如何填充数组内的对象和猫鼬中的另一个数组中的所有用户字段

如何仅在猫鼬中使用聚合填充嵌套在对象数组中的字段?

猫鼬中的填充方法(节点js)