Does not show any collection in mongodb - 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.

Related

Why the result of db.user.count() is 0 in mongo?

as you can see in the image below I just create an user even though it still gave 0 , and the same thing with collections when I run show collection it shows me one , but when when I do db.collection.count() gave me 0.
the image for db.collection.count()
I do have one document on this collection here is image from Mongo compass
As you can see there is no "user" collection in this database , there is only one collection and its name is "Databases_for_tp" so counting documents in not existing collection will show always 0.
You misenterpreted the meaniing of count() command , it counts how many documents there is in single collection , not how many collections there is in chosen database.
You can find the users when you create them in the admin database even they are created to authenticate in different database.
check:
use admin
show collections
db.system.users.count()
Note also it is a good practice to add code as text and not as pictures so it is easier to interpret.

mongoose list collections of a specific database

I use the following code to list the collections of the current database, but how to list the collections of another one?
mongoose.connection.db
.listCollections()
.toArray()
.then(coll => console.log({ coll }));
At one time you can see collections regarding only one Database. So if you need to list another database collections, you just need to change the corresponding DB name in the DB connection function. (Simply you can change the connection URL DB name)
Note:- If particular DB has different username and password than current DB, you need to change it on the connection URL as well.

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()

Delete data from a collection in mongo db by using multiple conditions

I am new to mongo db
using i have to delete data from a table like
select from table where id=12 and browser =GC
But I dont know how to perform this in mongo db?
Use remove
db.collectionName.remove({'_id':12,'browser':"GC"})
Can provide more accurate answer if you show your collection and documents .

mongodb collection

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()