I'm trying to save a collection in my Meteor application.
So far I have this simple bit of code:
MyCollection = new Mongo.Collection("Dogs"); console.log(MyCollection.find({}));
This creates the collection as the console log confirms its existence. However, it isn't creating it in my specified Mongo URL.
I am running the application locally and specifying the Mongo URL with the following command:
MONGO_URL='<MyURL>' meteor
This string seems to work as when I signed up a user to the application a users collection was created as well as a meteor_accounts_loginServiceConfiguration collection within my specified database.
However, the code to create the collection 'Dogs' - Which is attached to a button click - Fails to create a new collection in the database but it's being created elsewhere.
I can only assume this is being created in the local Mongo provided by Meteor?
Also, I have tried setting up my settings.json file with the following:
{
"galaxy.meteor.com": {
"env": {
"MONGO_URL": "<MongoURL>"
}
}
}
And pointed the application to it during deployment. This also seems to have worked as creating a new user updates the 'Users' collection in my database. However, the issue still persists that the 'Dogs' collection is not being created in my specified Mongo database.
Does any body know why this may be the case? What am I missing?
Many thanks for your help,
G
UPDATE
I tried this method:
var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });
Which I found here: Using Multiple Mongodb Databases with Meteor.js
But this fails to create any collection in my MongoDB either.
Another Update
After looking at this question:
https://forums.meteor.com/t/how-to-view-collection-contents-using-console-log/23852/12
I tried the following:
I created a folder called server and within it a file called main.js which contained the following:
const Tasks = new Mongo.Collection('tasks');
Tasks.insert({_id: 'my-todo'});
if (Meteor.isServer) {
// This code only runs on the server
Meteor.publish('tasks', function() {
return Tasks.find();
});
}
Within my application on the client side I added the following code to my JS file:
if (Meteor.isClient) {
Meteor.subscribe('tasks');
}
According to the help forum this should work.
It works partially - The tasks collection is created in my specified Mongo URL and the task is inserted into it.
However, I don't know how to display this now on the client side.
Any suggestions?
Thank you!:)