mongodb collection - mongodb

I want to list all the collection, created under my database. I know the query
db.getCollectionNames();
But it listing only - [ "system.indexes", "system.users" ]
I tried
show collections
it listed following - system.indexes, system.users
It is not showing me my collections. How can i see all my collections ?

To see all the collections in the current database using the mongo shell:
db.getCollectionNames() returns the collections in the current database as a JavaScript array
show collections prints the collections in the current database as a list
If you aren't seeing the expected collections, you can check the current database with:
db.getName()
If you want to see all collections in all databases, here's some JavaScript that should do the trick:
db.adminCommand("listDatabases").databases.forEach(function (d) {
sdb = db.getSiblingDB(d.name);
print("Collections in database: "+d.name);
printjson(sdb.getCollectionNames())
print("")
})

I think there must be more than one driver out there.
I did
npm install mongodb
to install the driver and I think the driver I get is the one documented here
http://mongodb.github.io/node-mongodb-native/2.0/api/
It does not have getCollectionNames() or collectionNames() but it does have collections()

Related

Why no result of db.database.find() although data is there in mongodb?

I have imported data into mongodb from csv files which has a million records using mongoimport utility:
show dbs;
admin 0.000GB
ded 0.305GB
local 0.000GB
visitors 0.000GB
db.ded.find();
Why is there no rows in the ded database when there is data of 0.305GB?
How can I see this data?
Any inputs on this would be great help.
find() does not return any document because you are using the test database which does not have collection named ded.
By default, MongoDB connects to the test database and from the output of show dbs, it clear that ded is a database. To query the documents in the collections in that database, you first need to switch to it using:
use ded
Then show collections to list the collections in that database.
Also note that after switching, db is the instance of your database (here ded) so you will be querying your collection like this:
db.collectionname.find()
So if your collection's name is "flights",
db.flights.find()
See Working with the mongo Shell
You can use this utility (http://3t.io/mongochef/) it is easy to use and helpful for those who are new to Mongodb.
While the answer to your question is the record is not showing through database name.First write db.printCollectionStats() which gives you the collection name. then use the collection name to show the data.
Example
db.printCollectionStats() will give you
xyz
then write
db.xyz.find()

Does not show any collection in mongodb

I am a beginner in mongodb. 2 days before I created a db named inventory and inserted collection too. But today I want get all collections in Inventory
I typed
db.inventory.find()
but it didn't show anything... what's the reason?
If ur db is inventory just use the following commands
use inventory
show collections
This would list u all the collections u've created inside this db .
db.collection_name.find()
will list u all data(documents) u've created in it
In short:
use inventory
show collections
By default the mongo shell connects to the database test. so you have to type use inventory to switch to your desired database (show databases returns a list of all created databases if you are facing errors with typos). After switching to the correct database type show collections to get a list of all created collections in your current database.

How can I drop a mongo collection in Erlang with the mongodb-erlang driver?

How can I drop a MongoDB collection in Erlang using the mongodb-erlang driver (https://github.com/mongodb/mongodb-erlang)?
I didn't find anything in the docs: http://api.mongodb.org/erlang/mongodb/
I'm writing tests that create collections with different names and I want to drop them when the tests are finished. I can delete all the documents in a collection, but I want to drop the collection itself.
Use the mongo_query:command/3 function and the document form of the drop command:
1> mongo_query:command({Db, Conn}, {drop, 'foo.bar.baz'}, false).
{nIndexesWas,1.0,msg,<<"indexes dropped for collection">>,
ns,<<"foo.bar.baz">>,
ok,1.0}
Takes a regular connection, not a replset connection.
mongo_query:command/3 function:
http://api.mongodb.org/erlang/mongodb/mongo_query.html#command-3
Document form of MongoDB drop command function:
http://docs.mongodb.org/manual/reference/command/drop/#dbcmd.drop

db.getCollectionNames() equivalent in pymongo

mongodb db.getCollectionNames()command will give you all collection names what are all there in the current db, in a list.
I want the same output using pymongo. I googled some time and couldn't find anything like that.
Is anything like that exists ?
collection_names()
Will show you the collections of the current database.
From documentation:
collection_names()
Get a list of all the collection names in this database.
[read more]
Since pymongo 3.7, collection_names() is deprecated and the correct way to get collection names is list_collection_names()

How can I list all collections in the MongoDB shell?

In the MongoDB shell, how do I list all collections for the current database that I'm using?
You can do...
JavaScript (shell):
db.getCollectionNames()
Node.js:
db.listCollections()
Non-JavaScript (shell only):
show collections
The reason I call that non-JavaScript is because:
$ mongo prodmongo/app --eval "show collections"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement #(shell eval):1:5
$ mongo prodmongo/app --eval "db.getCollectionNames()"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
[
"Profiles",
"Unit_Info"
]
If you really want that sweet, sweet show collections output, you can:
$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info
> show collections
will list all the collections in the currently selected DB, as stated in the command line help (help).
How do I list all collections for the current database that I'm using?
Three methods
show collections
show tables
db.getCollectionNames()
To list all databases:
show dbs
To enter or use a given database:
use databasename
To list all collections:
show collections
Output:
collection1
collection2
system.indexes
(or)
show tables
Output:
collection1
collection2
system.indexes
(or)
db.getCollectionNames()
Output:
[ "collection1", "collection2", "system.indexes" ]
To enter or use given collection
use collectionname
> show tables
It gives the same result as Cameron's answer.
Apart from the options suggested by other people:
show collections // Output every collection
show tables
db.getCollectionNames() // Shows all collections as a list
There is also another way which can be really handy if you want to know how each of the collections was created (for example, it is a capped collection with a particular size):
db.system.namespaces.find()
First you need to use a database to show all collection/tables inside it.
>show dbs
users 0.56787GB
test (empty)
>db.test.help() // this will give you all the function which can be used with this db
>use users
>show tables //will show all the collection in the db
Try:
help // To show all help methods
show dbs // To show all dbs
use dbname // To select your db
show collections // To show all collections in selected db
You can use show tables or show collections.
The command used for displaying all the collections in the MongoDB database is
show collections
Before running the show collections command you have to select the database:
use mydb // mydb is the name of the database being selected
To see all the databases, you can use the command
show dbs // Shows all the database names present
For more information, visit see Getting Started.
The following commands on mongoshell are common.
show databases
show collections
Also,
show dbs
use mydb
db.getCollectionNames()
Sometimes it's useful to see all collections as well as the indexes on the collections which are part of the overall namespace:
Here's how you would do that:
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
Between the three commands and this snippet, you should be well covered!
I think one of the biggest confusions is the difference between what you can do with mongo (or an interactive/hybrid shell) vs. mongo --eval (or a pure JavaScript shell). I keep these helpful documents handy:
Differences Between Interactive and Scripted mongo
Mongo Shell Command Helpers
Here is an example of scripting what you might otherwise do with show commands:
# List all databases and the collections in them
mongo --eval "
db.getMongo().getDBNames().forEach(
function(v, i){
print(
v + '\n\t' +
db.getSiblingDB(v).getCollectionNames().join('\n\t')
)
}
)
"
Note: That works really well as a one-liner. (But it looks terrible on Stack Overflow.)
mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
1. show collections; // Display all collections
2. show tables // Display all collections
3. db.getCollectionNames(); // Return array of collection. Example :[ "orders", "system.profile" ]
Detailed information for every collection:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
For users with the required access (privileges that grant listCollections action on the database), the method lists the names of all collections for the database.
For users without the required access, the method lists only the collections for which the users has privileges. For example, if a user has find on a specific collection in a database, the method would return just that collection.
To list collections list based on a search string.
db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })
Example: Find all collection having "import" in the name
db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
If you want to show all collections from the MongoDB shell (command line), use the shell helper,
show collections
that shows all collections for the current database.
If you want to get all collection lists from your application then you can use the MongoDB database method
db.getCollectionNames()
For more information about the MongoDB shell helper, you can see mongo Shell Quick Reference.
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
connect with the MongoDB database using mongo. This will start the connection.
then run show dbs command. This will show you all exiting/available databases.
then select the database you want. In the above it is anuradhfirst. Then run use anuradhfirst. This will switch to the database you want.
then run show collections command. This will show all the collections inside your selected database.
For switching to the database.
By:
use {your_database_name} example:
use friends
where friends is the name of your database.
Then write:
db.getCollectionNames()
show collections
This will give you the name of collections.
On >=2.x, you can do
db.listCollections()
On 1.x you can do
db.getCollectionNames()
List all collections from the mongo shell:
db.getCollectionNames()
show collections
show tables
Note: Collections will show from current database where you are in
currently
show collections
This command usually works on the MongoDB shell once you have switched to the database.
In case anyone uses Python & PyMongo:
db.list_collection_names()
For MongoDB 3.0 deployments using the WiredTiger storage engine, if
you run db.getCollectionNames() from a version of the mongo shell
before 3.0 or a version of the driver prior to 3.0 compatible version,
db.getCollectionNames() will return no data, even if there are
existing collections.
For further details, please refer to this.
Use the following command from the mongo shell:
show collections
I use listCollections (supports MongoDB 3.0 and up) for this purpose.
Example:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });
To fetch more information like the index of the collection:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });
To print just the collection names:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})
I feel this provides more flexibility.
Read more: listCollections
show collections
or
show tables
or
db.getCollectionNames();