I'm a beginner with MongoDB and I've some questions:
When I'm connected to Mongo, and i execute show dbs I see 2 databases: admin and local. What's their role? Then if I execute an insert command like db.foo.insert({"value":"mongo"}), the test database appears. Why? How can i specify a custom name for a database?
With show dbs I get the databases (somehow like show databases in sql), how can I then list the collections inside a database (I would use show tables in sql)?
When executing a command, the MongoDB tutorial always uses the db object. Is it the main object (a sort of "connection" object) that has to used for executing commands or it's something else?
Thanks!
admin and local contain various settings local to the server, like users who are authenticated to connect. Under beginner usage, you shouldn't need to worry about them at all. By default you connect to a database named test. To connect to a new database, just use databasename from the mongo command line, or mongo databasename from your OS shell.
use [database_name] and then show collections
The db object is your root handle to the currently-selected database on the mongo commmand line. The command line is really just a Javascript command line, and there are various mongodb-specific objects and functions exposed that let you do stuff. Try help() for a full listing.
Related
I have an existing database for a discord bot in MongoDB Compass v1.28.1 I want to transfer all the data in the database to mongodb atlas because of its more extensive functionality and to not have to wait for compass to take ages to load each time I open it. However when I follow the steps to connect that are provided in Atlas, the pop-up that's supposed to appear when I copy a path to the clipboard doesn't appear, and nothing happens. I tried to connect through my app in VSCode, the same way I did for Compass, using mongoose. Still no collections are loading or any data being stored. I have made my schemas etc. which work perfectly fine in Compass...
Migration to Atlas is documented at https://docs.atlas.mongodb.com/import/
To save you some reads, you have to options - export/import and mongodump/mongorestore.
I would recommend to try export/import first. It's built into Compass https://docs.mongodb.com/compass/current/import-export/ and must be simpler to use considering limited experience with mongo. It's UI oriented so just follow the click-through guide in the documentation.
Unfortunately it has some limitations related to data type conversion from BSON to JSON and may be a bit tedious if you have large number of collections.
In this case you will need to follow CLI mongodump/mongorestore way #barrypicker suggested in the comments. Both commands are available in cmd and PowerShell consoles.
First you backup your local database https://docs.mongodb.com/v4.2/reference/program/mongodump/:
mongodump --uri="mongodb://username:password#localhost:27017/discordbot"
username and password are the ones you use in compass to connect to the source database.
It will create dump directory with all collections you have.
Then you have to upload the backup to Atlas:
mongorestore --uri="mongodb+srv://username:password#cluster.tenant.mongodb.net/database" dump/
username and password are the ones you use to connect to atlas cluster, listed in the "Security/Database Access" section.
You can get the exact subdomains for the --uri part from Atlas. In the dashboard click "Connect" button for the cluster you want to connect to, then choose "shell" as the connection method in the connection pop-up:
I am using MongoDB. All I want to do is add a collection to a database I have created. I verified before I even started that the database existed by running the command "show dbs". However, when I try to add the collection to that database, it says that this database is undefined. Any thoughts or suggestions? Here were the commands I typed in terminal
use myDB
myDB.myCol.insert({"id":"1"})
the response I got from this command is "myDB is not defined" even
though I verified that it exists with the command "show dbs".
Use db instead of the name of the database in your insert command:
db.myCol.insert({"id":"1"})
use MyDB sets which database db refers to.
We are creating Meteor-based Mongo database manager and we need the ability to "unmount" (remove from system) all collections when we switch databases.
Example:
I'm managing database called dbA. We have all collections for that database created using Mongo.Collection() on server and on client side.
I want to switch database to dbB. I need to unmount all Collections of dbA and mount those of dbB. Reason: dbB could have a collection of the same name as dbA (and usually does)
Is there a way to do this?
Thanks!
You may be able to accomplish this by publishing the necessary data from the new database.
Here's a discussion from a similar question on the Meteor Forums (note the proposed solution at the end):
https://forums.meteor.com/t/switch-database-while-meteor-is-running/4361/5
hi i think you can do with
db.copyDatabase()
run the shell command in the backend from meteor server and execute the copy database command. and after the database is copied you can remove the previous collection.
more detail about copyDatabase() is here
https://docs.mongodb.org/manual/reference/method/db.copyDatabase/
I have a script that I wrote to query mongodb in python I am using PyMongo. I am trying to use this script to connect to a remote MongoDB server and then run the query within the script and then I want to be able to dump the data I get back from the mongodb into a file.
What are the parameters I need to have at the top of the script to connect to this database, use my username and password, switch to the correct database and then run the query?
Couple of options.
First you could provide a MongoDB URI which can be provided to the MongoClient as an argument. Then you can switch as needed using the standard methods for getting a database once connected.
Alternately, you can connect as normal, use the getting a database once connected method to get the desired database and then use the authenticate function to authenticate against the database.
I apologize if this is easily google-able or already asked before.
I have tried googling, but I get hits to explain how to get all collections.
I am using MongoDB 2.4.8
Run show databases or show dbs in the Mongo shell:
$ mongo
MongoDB shell version: 2.4.8
connecting to: test
> show databases
db1 0.203125GB
local 0.078125GB
test 0.203125GB
This will list your databases.
To list the available databases, use the helper show dbs. See also How can I access different databases temporarily? to access a different database from the current database without switching your current database context (i.e. db..) source