I am new to mongodb and node.js. I have a simple node.js app that in order to run I have to run the command
mongod
to make sure mongodb is running.
locally this works fine.
now I am moving to heroku, I createrd a Procfile where I declare :
web: node http.js
so this take care of starting node.js.
Now how can I call the command
mongod
on heroku?
Update
before the heroku requirement this is how I used to initialize my mongodb :
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure,
ObjectID = mongo.ObjectID;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sampledb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'sampledb' database");
db.collection('sample', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'sample' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
in order to accomodate heroku here is how I initialize mongodb :
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure,
ObjectID = mongo.ObjectID;
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/sampledb';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('sample', function(er, collection) {
if (err) {
console.log("The 'sample' collection doesn't exist. Creating it with
sample data...");
populateDB();
}
});
});
Now this throws an error because db is not recognized. I think I am missing a conceptual issue here, I am new to this world all together.
any help is appreciated.
You don't call the command mongod on heroku.
You'd add one of the mongo add-ons from addons.heroku.com to your app, then have your code connect to the credentials in the config variable exported to your app by the add-on provider.
Related
I am using MongoDB version 2.4.14 on a Raspberry Pi (running the latest Raspbian). I have a simple test file trying to get mongo to work:
let url = 'mongodb://172.20.0.239:27017/'
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url,{ useUnifiedTopology: true }, function(err, db) {
if (err) throw err;
var dbo = db.db(<database>);
var cursor = dbo.collection("Players").find();
if (cursor == null) {
console.log('cursor nulo');
} else {
console.log('cursor n nulo');
var strArray = [];
cursor.each(function (err, item) {
if(item!=null){
strArray.push(item.datetime+' '+item.onlineUsers);
console.log(item.datetime+' '+item.onlineUsers);
}
else{
dbo.close();
}
});
}
});
I am getting the error:
MongoServerSelectionError: Server at 172.20.0.239:27017 reports maximum wire version 0, but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)
Is there something wrong with my code?
Modern MongoDB drivers support MongoDB server 2.6 and newer. You are using a 2.4 server.
If possible I recommend upgrading your server to at least 2.6. If this is not possible, you need to hunt down an old (really old at this point) version of the driver that supports 2.4.
I installed mongoDB localy as a service by tutorial. But if i connected to DB on sample from w3school
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
there is no error. When i opened Compas i didn't see new DB "mydb". But if i inserted some data to mydb across application and wrote result to console.log then are theess data returned. But in compas are still not visible. Did you meet with this problem ? Thanks.
There must be some Meteor 1.3 examples of opening a mongo database using
the native driver. Use the MongoDB Node.JS Driver to access collections in
another local database for example. The new meteor 1.3 guides claim it is all possible.
Here's a slightly more updated answer:
import { MongoClient } from 'mongodb';
// Meteor offsets Mongo port by 1, so in case your application is running on 3000
const connection = await MongoClient.connect('mongodb://localhost:3001/', {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 1,
});
const db = await connection.db('testdb');
// then you may create your collection of choice
db.collection('testcollection');
FYI, you can also directly access the Mongo driver like so
import { MongoInternals } from 'meteor/mongo';
MongoInternals.defaultRemoteCollectionDriver()
.mongo.db
import { mongodb } from 'mongodb';
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/test",function(err,db) {
var collection = db.collection('test');
var doc1 = {'hello':'doc1'};
var doc2 = {'hello':'doc2'};
var lotsOfDocs = [{'hello':'doc3'}, {'hello':'doc4'}];
collection.insert(doc1);
In meteor, if you start meteor mongo, you get a shell, where you can access the database purely as mongo.
project :: (master*) ยป meteor mongo
MongoDB shell version: 2.6.7
connecting to: 127.0.0.1:3001/meteor
Mongo-Hacker 0.0.4
meteor:PRIMARY> db.users.find().count()
6
meteor:PRIMARY>
I want to access the item that is like the db, but with Meteor. For a simple example,
function getTableEntityCount(tablename) {
return db[tablename].find().count();
}
Is this possible in Meteor?
You can try with the mongodb native driver.
// Server code
//Typical require
var mongodb = Meteor.npmRequire("mongodb"), //using arounfa meteorhacks:npm
db = mongodb.Db,
mongoclient = mongodb.MongoClient,
Server = mongodb.Server,
db_connection = new Db('cats', new Server("127.0.0.1", 27017 {auto_reconnect: false}));
db.open(function(err, db) {
db.authenticate('<username>', '<password>', function(err, result) {
//return db[tablename].find().count();
});
});
This is just conceptual code.
I think now you can use this method:
MyCollection.rawCollection()
https://docs.meteor.com/api/collections.html#Mongo-Collection-rawCollection
I am supposed to find all the collections stored in the a mongo database.
require('../app/models/schemas'); //loading application schemas
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
var collections = db.collections();
console.log(collections);
Here collections prints a combined 'json' data of all the schemas.
But i want to find all the collections stored in the mongo test database.
How to achieve it with mongoose?
You can use the collectionNames function to return a list of collections.
db.on('open', function(){
mongoose.connection.db.collectionNames(function(error, names) {
if (error) {
throw new Error(error);
} else {
names.map(function(cname) {
console.log(cname.name);
});
}
});
});
=> database1.system.indexes
=> database1.users
=> database1.posts
make sure mongodb is up and running, in the terminal:
$ mongo
show dbs
use [db_name]
show collections
If you know in wich db your collections are :
$ mongo [db_name]
show collections