ReferenceError: require is not defined in MongoDB shell - mongodb

I try to connect MongoDB from Mongo client on windows command (Window 8.1). When I use require() in javascript, I have error as below. Does any one has same issue? Did I miss any require related npm installation? How can't MongoDB shell find require function?
C:\tutorial\nodeMongoAngular-master\lesson2>mongo
MongoDB shell version: 3.0.1
connecting to: test
var MongoClient = require('mongodb').MongoClient;
2015-04-30T14:33:25.812-0400 E QUERY ReferenceError: require is not defined
at (shell):1:19

You are confusing the mongo administrative shell with the Node.js driver. While both environments happen to use JavaScript, the mongo shell has more limited I/O support and is not intended to be used as a driver for application development.
If you want to write Node.js applications using the MongoDB driver (as per your example code), you need to use the node interpreter. The Node.js driver documentation includes a Quickstart tutorial with examples that should help you get started.

#Scott Lee: if you're still looking for an answer, try running with command "node yourscript.js". Make sure mongod is running. 'mongo script.js' will work for pure mongo scripts without nodejs code.

Related

mongosh: how to connect with uuidRepresentation=javaLegacy?

I am connecting to my local host mongodb via moongo shell with the following command and getting the following error.
Command
mongosh "mongodb://username:password#localhost:27017/authDb?uuidRepresentation=javaLegacy"
Error
Connecting to: mongodb://<credentials>#localhost:27017/authDb?uuidRepresentation=javaLegacy&directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.2.3
MongoParseError: option uuidrepresentation is not supported
My requirement is to execute mongo queries that involves Java Legacy UUIDs and the expect the query results to be represented like that.
I can achieve this via tools like Robo3T though.
But I am a specific case where I must do it via mongo shell.
I cannot find any support for this elsewhere.

Mongo 3.4 in Meteor

Is that possible to use Meteor with Mongo 3.4 for now? And if so, how to manage that?
I've tried to change packages file, but realised that version of mongo package is not a real Mongo's version.
This is indeed possible by taking care of mongodb yourself instead of letting meteor handle it. To do so you will have to fire up a mongod instance and set the MONGO_URL variable when you run meteor in development mode. In production mode you will be running a standalone mongodb instance anyway, so you will not need to change anything there.
As a reference, see this discussion on the meteor forums: https://forums.meteor.com/t/running-mongodb-3-4-locally-with-meteor/34242
To set an environment variable when running meteor in development mode requires the following:
MONGO_URL=mongodb://localhost:27017/meteor meteor
In this case you are accessing the meteor database (/meteor) on the local mongodb instance (localhost) on the standard port (:27017).

Iron router cannot open mongo console

I am using iron router and unfortunately every time i cd into a directory generated by iron cli,i am notified i am not in a meteor directory.
So i have tried iron mongo and opens
> meteor mongo
MongoDB shell version: 2.6.7
connecting to: 127.0.0.1:3001/meteor
but has been this way for the last five minutes. Is there an alternative way to access the mongodb that comes with meteor or what should be done incase you are using iron router?.
In my case,i just want to see how many collections i have like
show collections
I think is best way for you it is use some Admin UI. Check "mongodb admin ui" in google for variants.
For my part I can recommend:
Robomongo
Its is really perfect mongo manager. Native and cross-platform MongoDB manager
Whatever platform you use today — Robomongo is available for you. Distributed as a native application, fast and snappy Robomongo uses very little of your machine resources.
But if you find some critical for you bugs, try another perfect tool
MongoVUE
MongoVUE is an innovative MongoDB desktop application for Windows OS that gives you an elegant and highly usable GUI interface to work with MongoDB. Now there is one less worry in managing your web-scale data.
Grab the local mongo connection string first then connect as follows
God#God-HP-EliteBook-2540p:~/crud$ iron mongo
> meteor mongo
MongoDB shell version: 2.6.7
connecting to: 127.0.0.1:3001/meteor
^C
God#God-HP-EliteBook-2540p:~/crud$ mongo mongodb://127.0.0.1:3001/meteor
Feed commands
God#God-HP-EliteBook-2540p:~/crud$ mongo mongodb://127.0.0.1:3001/meteor
MongoDB shell version: 3.2.4
connecting to: mongodb://127.0.0.1:3001/meteor
meteor:PRIMARY> show collections
Cannot use 'commands' readMode, degrading to 'legacy' mode
crud
meteor_accounts_loginServiceConfiguration
roles
system.indexes
users
meteor:PRIMARY> show dbs
admin (empty)
local 0.063GB
meteor 0.031GB
meteor:PRIMARY>

Mongodb "auth fails" with mongodb php driver and new php library

Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.
In the following code, the connection string follows the pattern mongodb://user:password#mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.
However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179
Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.
Environment:
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6
I just had the same error and found out that I had to place the database-name in the connection string.
The documentation here says:
If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
And the user I'm using has no rights to the admin-database, so this is why I received the authentication error.
I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.
So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.

How does Meteor work without installing MongoDB?

I have started looking at Meteor and tried out some examples, but I'm puzzled by something: I have installed Meteor and not MongoDB on my machine, but Meteor seems to create its own instance of MongoDB.
How does this work?
Am I able to develop a separate application that can also perform CRUD operations on this database that Meteor is spinning up?
Meteor is shipped with a MongoDB installation
I think you can. The meteor mongo command gives the MongoDB URL to the database Meteor spins up.
meteor command will start its own instance of MongoDB.
You can then use meteor mongo to connect to the MongoDB database, which is usually running on the application's port+1, for example, app:3000 -> mongodb#:3001. So you could use any MongoDB tool to connect (for example, robomongo).
$ meteor mongo
MongoDB shell version: 2.4.9
connecting to: 127.0.0.1:3001/meteor