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

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.

Related

query in mongodb atlas to verify the existence of multiple specific documents in a collection

I have a mongodb collection called employeeInformation, in which I have two documents:
{"name1":"tutorial1"}, {"name2":"tutorial2"}
When I do db.employeeInformation.find(), I get both these documents displayed. My question is - is there a query that I can run to confirm that the collection contains only those two specified documents? I tried db.employeeInformation.find({"name1":"tutorial1"}, {"name2":"tutorial2"}) but I only got the id corresponding to the first object with key "name1". I know it's easy to do here with 2 documents just by seeing the results of .find(), but I want to ensure that in a situation where I insert multiple (100's) of documents into the collection, I have a way of verifying that the collection contains all and only those 100 documents (note I will always have the objects themselves as text). Ideally this query should work in mongoatlas console/interface as well.
db.collection.count()
will give you number of inserts once you have inserted the document.
Thanks,
Neha

In MongoDB find out when last query to a collection was? (Removing stale collections)

I would like to find out how old/stale a collection is, I was wondering if there was a way to know when the last query was made to a collection, or even get a list of all collections last access date.
If your Mongodb collection document _id is of the following format "_id" : ObjectId("57bee0cbc9735bf0b80c23e0") then Mongodb stores the create document timestamp.
This can be retrieved by executing the following query
db.newcollection.findOne({"_id" : ObjectId("57bee0cbc9735bf0b80c23e0")})._id.getTimestamp();
the result would be an ISODate like this ISODate("2016-08-25T12:12:59Z")
find out how old/stale a collection
There is no predefined libraries available in mongodb to track the oldness of a collection. But it is doable by maintaining a log where we can keep an entry when we are accessing a collection.
References
ObjectID.getTimestamp()
Log messages
Rotate Log files
db.collection.stats()

Meteor Mongodb first object array not selectable with dot notation

I have tried and tried on Meteor and on Robomongo (Mongodb) to select objects with dot notation.
I would like to be able to filter team.0.wageringStats.wageringStraightSpread objects (sometimes subjects can be fields or arrays - thats another issue)
In the first image I can select team.wageringStats.wageringStraightSpread and get back all the subOjects of team (team has siblings not shown in images)
The second image I tried team.0.wageringStats.wageringStraightSpread and I get no fields.
Lastly i tried team.[0].wageringStats.wageringStraightSpread and
team[0].wageringStats.wageringStraightSpread and get the same result : 0 fields
I am at a loss and would like some help. Thank you
I am not sure what you are trying to do now? Because in your first command, you already have a list of team that match your criteria and then, put it into the loop of meteor to process. Why do you need to find only the first one ? By the way, in order to select the nth of the result set in mongodb, you will need something like skip and limit
db.collections.find({'team.wageringStats.wageringStraightSpread':1}).limit(1).skip(0)
(in skip, you need to pass the offset you need to reach to)
Also, if you only care about the first one, findOne is the one you need to do the query
db.collections.findOne({'team.wageringStats.wageringStraightSpread':1})
Be aware that the syntax of mongodb and meteor for querying is a bit different

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.

why a collection exists but can not find in show collections?

In the mongo shell, I can find records from collection stat by using such commands:
use gm;
gm.stat.find({});
but stat is not listed in show collection results.
Any collection virtually exists all the time (i.e. you will not get an error saying that "you did not create a collection"). As soon as you insert the first document into a collection this will exist also physically (will be created on the disk). So if you really want to make sure a collection exists use:
db.getCollectionNames()
This will show you only the collections that had at least one document inserted into them, even if they are currently empty.
Once physically created, a collection can be deleted using the drop command:
db.myColl.drop()
This will delete it physically but the "virtual" one will still be there.
As for your example, running:
db.stat.insert({}); print("Collection stat exists:" + (db.getCollectionNames().indexOf("stat") !== -1));
will tell you:
Collection stat exists: true