在 Meteor.js 中使用多个 Mongodb 数据库 [英] Using Multiple Mongodb Databases with Meteor.js

查看:33
本文介绍了在 Meteor.js 中使用多个 Mongodb 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2 个 Meteor.Collections 是否可以从 2 个不同的 mongdb 数据库服务器中检索数据?

Is it possible for 2 Meteor.Collections to be retrieving data from 2 different mongdb database servers?

Dogs = Meteor.Collection('dogs')        // mongodb://192.168.1.123:27017/dogs
Cats = Meteor.Collection('cats')        // mongodb://192.168.1.124:27017/cats

推荐答案

更新

现在可以连接到远程/多个数据库:

Update

It is now possible to connect to remote/multiple databases:

var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });

其中是一个mongodb的url,比如mongodb://127.0.0.1:27017/meteor(带数据库名)

Where <mongo_url> is a mongodb url such as mongodb://127.0.0.1:27017/meteor (with the database name)

目前有一个缺点:没有 Oplog

There is one disadvantage with this at the moment: No Oplog

目前这是不可能的.每个流星应用都绑定到一个数据库.

At the moment this is not possible. Each meteor app is bound to one database.

有几种方法可以解决这个问题,但它的价值可能更复杂:

There are a few ways you can get around this but it may be more complicated that its worth:

在您的其他流星应用程序中(例如在同一台机器上的 6000 端口运行).您仍然可以具有反应性,但您需要通过方法调用来代理插入、删除和更新

In your other meteor app (example running at port 6000 on same machine). You can still have reactivity but you need to proxy inserts, removes and updates through a method call

服务器:

Cats = Meteor.Collection('cats')

Meteor.publish("cats", function() {
    return Cats.find();
});

Meteor.methods('updateCat, function(id, changes) {
    Cats.update({_id: id}, {$set:changes});
});

您当前的 Meteor 应用:

Your current Meteor app:

var connection = DDP.connect("http://localhost:6000");

connection.subscribe("cats");
Cats = Meteor.Collection('cats', {connection: connection});

//To update a collection
Cats.call("updateCat", <cat_id>, <changes);

另一种选择 - 自定义 mongodb 连接

这里使用 node js mongodb 原生驱动.

Another option - custom mongodb connection

This uses the node js mongodb native driver.

这是连接到数据库,就像您在任何其他节点 js 应用程序中所做的一样.

This is connecting to the database as if you would do in any other node js app.

没有反应性可用,您不能使用 new Meteor.Collection 类型集合.

There is no reactivity available and you can't use the new Meteor.Collection type collections.

var mongodb = Npm.require("mongodb"); //or var mongodb = Meteor.require("mongodb") //if you use npm package on atmosphere

var db = mongodb.Db;
var mongoclient = mongodb.MongoClient;
var Server = mongodb.Server;

var db_connection = new Db('cats', new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize: 4}), {w:0, native_parser: false});

db.open(function(err, db) {
    //Connected to db 'cats'

    db.authenticate('<db username>', '<db password>', function(err, result) {
      //Can do queries here
      db.close();
   });
});

这篇关于在 Meteor.js 中使用多个 Mongodb 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆