【问题标题】:How can I save query data from another mongoDB to the collection of DB I am currently connected to?如何将查询数据从另一个 mongoDB 保存到我当前连接的数据库集合中?
【发布时间】:2020-07-19 23:26:10
【问题描述】:

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

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

并根据我当前使用的模型写入集合。

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

这是一个sn-p,希望对你有帮助。

    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();
}

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

【问题讨论】:

    标签: mongodb mongoose multiple-databases


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 2017-04-02
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多