如何将查询数据从另一个mongoDB保存到我当前连接的数据库的集合



我有两个数据库,我想从集合中的另一个数据库写入数据。

我连接到一个mongoDBmongoose.connect("mongodb://XXX:YYY@localhost:27017/realtime?authSource=admin"

并根据我目前使用的模型写入一个集合。

有一个键值对(它是一个时间戳(我需要来自运行在同一端口mongodb://XXX:YYY@localhost:27017/timetable?authSource=admin上的不同DB。

这是一个片段,希望能有所帮助。

EntityModel
.find({})
.limit(10)
.then((entities) => {
entities.forEach((entity) => {
// this is where I need to access the other DB
// to get arrivalTimetable from the timetable database for Each entity
tripData.stops.push({
stop_id: stu.stopId,
arrivalTimetable: arrivalTimetable,
arrivalRealtime: arrivalRT
});
const tripDelay = new TripDelay(tripData);
trip.save();
}

获取所需数据并将其保存到当前集合的好方法是什么?

$merge允许输出到不同的数据库。

最新更新