I'm getting different responses when inserting documents from Windows and OSX to an external MongoDB database.
Both systems are using mongodb driver v2.1.11 from https://www.npmjs.com/package/mongodb
Inserts are working fine on both but the result I get back is quite different.
OSX: { result: { ok: 1, n: 1, ...other data}, ops: [the inserted records] }
Win: [the inserted records]
Any idea why this is happening?
Steps to reproduce:
nodejs: 5.7.0
npm: 3.6.0
mongodb (from npm): 2.1.11
Given a nodejs application, install v2.1.11 of the official MongoDB driver for nodejs:
npm install --save mongodb#2.1.11.
From your nodejs application, import mongo, create a mongo client and connect to a MongoDB instance and insert a document into a collection:
import mongo from 'mongodb';
const MongoClient = mongo.MongoClient;
MongoClient.connect(mongoUrl, (err, db) => {
db.collection('someCollection').insert({ foo: 'foo'}, (insErr, result) => {
console.log(result); // Observe the result shape is different on Win/OSX
});
});
It turns out there was a package.json buried deep in my application that contains a lower version of the MongoDB driver.
Removing this pacakge.json and ensuring that everything was kept in the top level package.json has solved the problem for me.
Related
Recently, I got a new laptop and I am set up my development environment on there. Also, I copied a project I worked on on my old laptop and wanted to continue working on it on my new laptop. Nothing weird here, I would think. The server-side code in this case.
So I started with installing all the apps and programs, cloned my GitHub repo, booted up a docker container with the following command:
docker run -d -p 5432:5432 --name ulti-mate -e POSTGRES_PASSWORD=secret postgres
and ran npm install. I connected to the database using postico, just to have a little visual feedback, which connected instantly. Then started the server up using nodemon index.js and it seemed to start. Only I was missing one thing: Normally the server console logs Database connected and runs a bunch of insert queries. Went to Postico, nothing.
I've been going over the code of my database:
const Sequelize = require('sequelize');
const databaseUrl =
process.env.DATABASE_URL ||
'postgres://postgres:secret#localhost:5432/postgres';
const db = new Sequelize(databaseUrl);
async function syncDB() {
try {
console.log('Before sync');
await db.sync({ force: false });
console.log('Database connected');
} catch (error) {
console.error('error synching database', error);
}
}
syncDB();
and I noticed that it runs until it hits db.sync(). Before sync logs consistently. That's where it stops. It doesn't resolve at all. I tried assigning it to a variable, but nothing. For example, this does not log:
const a = await db.sync({force: false});
console.log("a:", a);
The weird thing is that it worked before on my old machine, so the problem can't be in the code. It must have something to do with my new development environment. I tried installing different versions of sequelize and pg in the repo, didn't help. I tried reinstalling postgresql with homebrew, but it's up to date.
If anyone has an idea what might be going wrong or something I might try to fix this issue, it would be greatly appreciated, because I'm slowly going mad.
I figured out the problem. I was running Node v14.4.0 (latest version at the moment). Downgrading to the latest LTS version (v12.18.1) fixed the issue. I'm not sure if this is a known issue, but I opened a ticket on the sequelize Repo.
Using phpstorm, trying to get mongoDB server up and running using nodeJS, i have mongo installed and have created the required folders on the c drive to store database, proceeded by going to command prompt and entering mongod, to get mongo db running....
checked npm ls,mongoose and mongo are listed fine, and are also present in my package json file.
when i try to run the index file from commmand prompt 'node index.js' i get the following error, see pic for details, i have included, the code used for setting up mongo... i have disable mongoserver from phpstorm repository, and use the one from the mongodb website
"mongodb": "3.0.0-rc0",
"mongoose": "^5.0.0-rc2",
var mongoose =require('mongoose');
//connect to mongoose
mongoose.connect('mongodb://localhost/quaver');
mongoose.connection.once('open', function(){
console.log('connection has been made , and is running')
});
mongoose.on('error', function(error){
console.log('Connection error',error);
});
error sorted by omitting the following lines from the semver.js file, in the node modules folder in the current project
line 203,204
//re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
//nodevar comparatorTrimReplace = '$a$2$3';
and line 232
// re[i] = new RegExp(src[i]);
mongoose now up and running
I would like to connect to Mongo DB using a webpack dev server. While the connection using the node mongodb driver and configuring in server.js is direct and straight forward, I am thinking of a way to do the same using webpack dev server in development (mainly for the hot loading advantage).
I understand that there is a way of achieving the same using a webpack middleware, but is there another easier and better way of doing it.
webpack dev-server is generally a simple Express or similar node.js server. It's essentially exactly the same as writing is on an express.js server.
npm install mongoose mongodb --save-dev
const mongoose = require('mongoose'); // Replace with import as desired
const mongoConnectString = 'mongodb://localhost/database-name-here';
mongoose.connect(mongoConnectString, (err) => {
if (err) {
console.log('Err, could not connect to the database.');
}
});
Replace the mongoConnectString as needed for developing or using databases that aren't local to your machine.
db.collection('session').remove({timestamp:{'$lte':a}},function(err, docs) {
console.log(err)
console.log(docs)
});
Version mongodb is 2.6.5, when I make this query from Robomongo visual manager it works normally but from node js it throws error "key $lte must not start with '$'"
I also faced similar issue with some downloaded code- shell worked properly but Node driver failed (there was no other external library like mongoose). My installed MongoDB version was 2.6.6 and the package.json had a mongodb entry as "~1.3.18". With a suggestion from someone, I changed the entry value to "~1.4.31" which resolved my problem.
Remove the quotes around $lte. It is not a key, but a directive.
So i am trying to use Node.js and Mongodb together and the goal is to use Node to get information and store it in a database with Mongodb. SO I have both Node and Mongdb intalled, and I install the Mongodb package with npm, this is the package mongodb recommends. But the problem I am having is that when I try to do
MongoClient.connect("mongodb://localhost:3000/exampleDb", function(err, db) {
if(err) { return console.dir(err); }else{
var collection =db.createCollection('test', function(err, collection) {}); }});
and I go to localhost:port_for express_server, but when the above code is supposed to run I get [Error: failed to connect to [localhost:3000]] in the Node console.Am I supposed to be running mongodb in the background or how is this supposed to work?
when you do
npm install mongodb
you only install a node.js client driver for mongodb.
in order for you script to run, you need to install and start a mongodb server on your box
check server installation procedures on http://docs.mongodb.org/manual/installation/
You seem pretty lost on what mongodb is and how to use it. Mongodb is a noSQL database.
Am I supposed to be running mongodb in the background
Yes, like mysql, you need the server to be installed and running, to use it. You need to do:
mongodb (the db server)
install server
start the server by typing mongod on terminal
check with mongo if it is running and you can connect to it.
node.js (the web server)
install node
install mongodb package
now test your code