Having problems with MongoDB collection named 'auth' - mongodb

I have a MongoDB collection named 'auth', and when I run:
db.auth.drop()
It of course conflicts with the db.auth() function that MongoDB provides.
Is there any other way to drop a collection?.

This issue arises when you try to access auth collection from mongo shell as it conflict with db.auth() you mentioned in the question. But using any driver you can perform CRUD on auth collection. I tried using its java driver, it works fine. That's why you faced no issue while using ORM as it is using any mongo driver internally.

Related

How do I rename a collection with Mongoid 7.x and MongoDB 3.x

I have a test app using Ruby 2.2.2 and am using Mongoid 7.0.0 and Moped 1.5.3 with MongoDB 3.6.2 - we are upgrading an ancient codebase using Mongoid 4.x and MongoDB 2.6 and found numerous breaking-changes in the API along the way
The most serious is we used to be able to do SomeModel.collection.rename however this API method now no longer exists (to my knowledge) and gives an undefined error
I've also tried the following:
Mongoid.default_client.command({ renameCollection: "test.some_collection", to: "test.some_collection2", dropTarget: true })
However this returns
Mongo::Error::OperationFailure: renameCollection may only be run against the admin database. (13)
From a command shell however, I'm able to issue:
db.some_collection.renameCollection("some_collection2")
And this works - this seems to be my only last recourse from what I can see, how would I issue this as a Moped command? (I'm not overly familiar with the syntax scheme)
Also, any reason why such a seemingly straightforward operation is apparently not exposed by Mongoid?
So found a way to do it - some of my collections are stored on different databases
# returns a hash of client config from mongoid.yml eg ["secondary_db", { "database" => "myDB", hosts => [xxx, xxx, xxx] }]
cfg = Mongoid.clients.select { |k,v| Mongoid.clients[k][:database] == MyModel.storage_options[:database] }.first
# now create a new Client instance using the right database
client = Mongo::Client.new(cfg[1]["hosts"], { database: cfg[1]["database"]})
client.command("$eval" => "db.my_collection.renameCollection('new_col_name')")

How to get Collections name from Cosmos DB using Mongo API

I am trying to execute Mongo API to perform CRUD operation on Azure Cosmos-DB.
I am running the query on Azure Data explorer.
This is a query that I am executing {db.getCollectionNames()}
I am facing {"code":500,"body":"{\"message\":\"There was an error processing your request. Please try again in a few moments.\",\"httpStatusCode\":\"InternalServerError\",\"xMsServerRequestId\":null,\"stackTrace\":null}"}
Can you please suggest the changes if I am doing something wrong here.
the Mongo Query area is not the same as a native MongoDB shell. That is, the only thing you can do within the query window is execute find() queries, and you only specify the filtering (between the {}). For example:
There's also the ability to open a mongo shell via the browser, where you can run queries, in the more traditional format for mongo:
With the browser-based shell, you can also do updates (e.g. db.families.update()) and deletes (db.families.remove()). But it doesn't support commands such as db.getCollectionNames().

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

Creating Indexed MongoDB during installation

I am creating an installation for a system using .NET and MongoDB. I wrote a batch to ensure indexes on DB but it would not work unless you have collection created. Is it a bad practice to do smth like:
db.Customers.save({username:"mkyong"})
db.Customers.remove({})
db.Customers.ensureIndex({SystemId:1,CampaignId:1,LocalIdentifier:1})
Use the createCollection command:
db.createCollection("Customers")
db.Customers.ensureIndex({SystemId:1,CampaignId:1,LocalIdentifier:1})