Search code examples
mongodbmeteorinsertexternal

insert data to external mongodb with meteor app


I have an instant of mongodb in the server , ana i connect my meteor app to this DB using that code : lib/connection.js

 MONGO_URL = 'mongodb://xxxxxxxx';
   var mongoClient = require("mongodb").MongoClient;
   mongoClient.connect(MONGO_URL, function (err, db) {

    if (err) {
            console.log('Unable to connect to the mongoDB server. Error:', err);
        } else {
            console.log('Connection established to cc', MONGO_URL);


             var collection = db.collection('test');
             var test1= {'hello':'test1'};
             collection.insert(test1);      
             db.close();
       }

    });

the connextion to the the external mongo is established and the collection test is created in the server but my app still connected to the the local mongo when i insert my collection: books:

thee code : collections/Books.js

Books= new Mongo.Collection('books');

BooksSchema= new SimpleSchema({

  name: {
    type: String,
    label: "Name"
    autoform:{
     label: false,
      placeholder: "schemaLabel"
        }
  },
  categorie:{
    type: String,
    label: "Categorie"
    autoform:{
     label: false,
      placeholder: "schemaLabel"
        }
  },


});

Meteor.methods({
deleteBook: function(id) {
  Cultures.remove(id);
}

});

Books.attachSchema(BooksSchema);

code client/books.html

 <template name="books">
    <p>add new books </p>
    {{> quickForm collection="Books" id="newBook" type="insert" class="nform" buttonContent='ajouter' buttonClasses='btn bg-orange'}}
    </template>

help bleaaaaaz


Solution

  • You should specify the database that is supposed to be used in MONGO_URL environment variable, not in your code. If you work locally start your application like this:

    MONGO_URL="mongodb://xxxxxxxx" meteor
    

    UPD

    Don't know about Windows. See this SO question.

    Looks like you should set env vars in windows like this:

    set MONGO_URL=mongodb://localhost:27017/mydbname