简体   繁体   中英

Mongoose populate from external results

Given that I have 2 completely different server and database.

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 }
    ...
]

Scenario:
From Server A , by fetching an api from Server B I got a list of ModelB 's result. I would want to populate ModelA by using the fetch result.

Let's say
ModelA 's find() result = resultA
ModelB 's find() result = resultB

My solution so far:

Solution A:
Flow: Fetch from Server B => Extract list of _id by using map => ModelA .find(listOfExtractedIds) => compare and merge result by nested loop resultA on resultB
*This is extremely inefficient as multiple loop is involved.

Solution B:
Flow Fetch from Server B => ModelA .populate( resultB , {path:"_id"})
* This is my ideal solution, but it does not work because Server A has no access to Model B and there's no ref involved.

Is there a better approach to this problem? Or is there a way for Solution B to work as expected?

*PS Server A has no access to ModelB which is on Server B .
*No ref specified on both model.

Well Solution A is the only solution in that situation, because apparantly two different databases cannot join data.

The only thing you could do is optimize the Solution A to improve performance, eg using some kind of a cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM