【问题标题】:Mongoose populate from external resultsMongoose 从外部结果中填充
【发布时间】:2021-12-10 17:06:06
【问题描述】:

鉴于我有 2 个完全不同的服务器和数据库。

Server A, Database A:
Database A's model as ModelA :{
    _id: ObjectId(),
    fieldA: any
}

Server B, Database B:
Database B's model as ModelB :{
    _id: ObjectId(),
    fieldB: any
}

Database A _id === Database B _id

Expected result:[
    { _id, fieldA, fieldB }
    ...
]

场景:
Server A,通过从 Server B 获取 api,我得到了 的列表ModelB的结果。我想使用获取结果来填充 ModelA

假设
ModelA 的 find() 结果 = resultA
ModelB 的 find() 结果 = resultB

到目前为止我的解决方案:

解决方案 A:
流程:从 Server B 获取 => 使用 map 提取 _id 列表 => ModelA.find(listOfExtractedIds ) => 通过 resultA 上的嵌套循环比较和合并结果 resultB
*由于涉及多个循环,因此效率极低。

解决方案 B:
服务器 B => ModelA.populate(resultB ,{path:"_id"})
* 这是我理想的解决方案,但它不起作用,因为 Server A 无法访问 Model B 并且不涉及ref

有没有更好的方法来解决这个问题?或者有没有办法让解决方案 B 按预期工作?

*PS Server A 无法访问 Server 上的 ModelB B.
*两个型号均未指定 ref

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    在这种情况下,解决方案 A 是唯一的解决方案,因为显然两个不同的数据库无法连接数据。

    您唯一能做的就是优化解决方案 A 以提高性能,例如使用某种缓存。

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 2023-03-23
      • 1970-01-01
      • 2012-01-22
      • 2018-01-06
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多