Mongo Collections with the same name in different databases

Asked

Viewed 72 times

0

I am making a prototype in Meteor that will need to connect to multiple databases and need to export to each of the databases a same Nucleon and I am not able to do the Publish of the Nucleons with different names to differentiate.

The idea is, I have several databases and in all there is Collection users, I need to do the Publish of all, each with a name.

Server-side

// Usuários do banco de dados 1
var RemoteDatabase1 = new MongoInternals.RemoteCollectionDriver(dababaseUrl1);
var Users1 = new Mongo.Collection('users', { _driver: RemoteDatabase1, _suppressSameNameError: true });

Meteor.publish('users1', function() {
  var UserCursor1 = Users1.find({});

  // this automatically observes the cursor for changes,
  // publishes added/changed/removed messages to the 'people' collection,
  // and stops the observer when the subscription stops
  Mongo.Collection._publishCursor(UserCursor1, this, 'users1');

  this.ready();
});

// Usuários do banco de dados 2
var RemoteDatabase2 = new MongoInternals.RemoteCollectionDriver(dababaseUrl2);
var Users2 = new Mongo.Collection('users', { _driver: RemoteDatabase2, _suppressSameNameError: true });

Meteor.publish('users2', function() {
  var UserCursor2 = Users2.find({});

  // this automatically observes the cursor for changes,
  // publishes added/changed/removed messages to the 'people' collection,
  // and stops the observer when the subscription stops
  Mongo.Collection._publishCursor(UserCursor2, this, 'users2');

  this.ready();
});

Client-side

// Usuários do banco de dados 1
const Users1 = new Mongo.Collection('users1');
Meteor.subscribe('users1');

var Users1Values = Users1.find({});
console.log(Users1Values);
Users1Values.forEach((user1Value) => {
  console.log(user1Value);
});

// Usuários do banco de dados 2
const Users2 = new Mongo.Collection('users2');
Meteor.subscribe('users2');

var Users2Values = Users2.find({});
console.log(Users2Values);
Users2Values.forEach((user2Value) => {
  console.log(user2Value);
});

The problem is that even though there is data in both databases, the data on the client side of the Collections are empty.

What would be the correct way to do Publish and subscribe in that case?

I have tried the approaches present in the answers to the questions:

But all resulted in empty customer side Customers.

  • Good Alex, try to do return of the cursor inside the Meteor.publish

1 answer

0

@Alex tries this (Code below) maybe can help you.

Basically what I did was create a Collection that will assist in the communication of server - Mongo and client - minimum. That’s because, when you make a subscribe the Content tells the server to send records to the client. The client stores these records in local Minimongo collections, with the same name as the collection argument used in the publication.

The client will see a document if the document is currently in the published record set of any of its signatures. If multiple publications publish a document with the same _id to the same collection, documents will be merged for the client. If the values of any of the upper level fields enter conflict, the resulting value will be one of the published values, chosen arbitrarily.

Currently, when several signatures publish the same document, only upper level fields are compared during merging. This means that if the documents include subfields other than the same upper level field, not all will be available on client.

// Nao Guarda na DB, porque nunca chamamos o metodo insert.
export const UsersAll = new Mongo.Collection('users.all');

// Server code
if (Meteor.isServer) {
  // Usuários do banco de dados 1
  const RemoteDatabase1 = new MongoInternals.RemoteCollectionDriver(dababaseUrl1);
  const Users1 = new Mongo.Collection('users1', {
    _driver: RemoteDatabase1,
    _suppressSameNameError: true
  });

  // Usuários do banco de dados 2
  const RemoteDatabase2 = new MongoInternals.RemoteCollectionDriver(dababaseUrl2);
  const Users2 = new Mongo.Collection('users2', {
    _driver: RemoteDatabase2,
    _suppressSameNameError: true
  });


  Meteor.publish("users.all", function usersAll(collectionId) {
    check(collectionId, String);

    let self = this;
    let newNode;

    // Formar uma lista exclusiva de utilizadores
    // _.union - https://underscorejs.org/#union
    newNode = _.union(Users1.find({}).fetch(), Users2.find({}).fetch());

    self.added('users.all', collectionId, {
      users: newNode
    });

    return self.ready();

  });
}

// client code

if (Meteor.isClient) {

  const collectionId = Random.id(); // Retornar um identificador único
  Meteor.subscribe('users.all',collectionId,onReady);

  function onReady() {
    var usersAll = UsersAll.findOne({_id: collectionId});
    usersAll = usersAll.users;
    console.log('usersAll: ', usersAll);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.