使用 meteor 应用程序将数据插入外部 mongodb

insert data to external mongodb with meteor app

我在服务器中有 mongodb 的瞬间,然后我使用该代码将我的 meteor 应用程序连接到此数据库: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();
       }

    });

建立了与外部 mongo 的连接,并在服务器中创建了集合测试,但是当我插入我的集合时,我的应用程序仍然连接到本地 mongo:书籍:

代码: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);

代码 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>

帮助 bleaaaaaz

您应该指定应该在 MONGO_URL 环境变量中使用的数据库,而不是在您的代码中。如果您在本地工作,请像这样启动您的应用程序:

MONGO_URL="mongodb://xxxxxxxx" meteor

UPD

不知道 Windows。看到这个 .

看起来您应该像这样在 windows 中设置环境变量:

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

好的,谢谢你 Ramil,我在 windows 上创建了一个新的系统环境变量,MOGO_URL 的值等于:mongodb://xxxxxxxx,它可以工作;应用程序连接到服务器中的数据库,并将数据插入其中。 现在我的问题是如何从该数据库中获取数据,我使用 Microsoft azure 将数据库存储为 API DocumentDB