mongo shell ReferenceError: database_name not found - mongodb

Hello I've been using mongodb with mongo shell and I keep having the same problem.
While the database called Videos exists in MongoDB Compass and I can use it and add data to it, when I try to access it from the mongosh with this command:
Videos.getCollectionNames();
or with this command:
Videos.GBvideos.find();
I keep getting this error:
ReferenceError: Videos is not defined
This happens both with the integrated mongosh (inside the Compass env.) and with the mongosh executable from the installation path.
Any ideas how to get over this error?
I need to use the shell as what I want to do can't be done with the Compass
Compass:

You can do this way in single command:
mongos> db.getSiblingDB("Videos").getCollection("GBVideos").find()
{ "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
{ "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
mongos>
Or if you need your way you must assingn to variable:
mongos> var Videos=db.getSiblingDB("Videos")
mongos> Videos.GBVideos.find()
{ "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
{ "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
mongos>

use Videos switched to db Videos
is the way to change the database from the mongo shell kudos to R2D2!!

Related

load() function mongo shell for updatemany

I am not getting output when i call update many via load(). My setVerboseShell(true)
However when i run the command explicitly in shell prompt it goes fine
MongoDB Enterprise > load('/opt/app/mongo/test.js') true
MongoDB Enterprise > setVerboseShell(true)
MongoDB Enterprise > load('/opt/app/mongo/test.js') true
MongoDB Enterprise > db.organizations.updateMany({"_id" : {$in : [ "120300007","160800001","175100009","193000031","201700008","235500001","237600001","24600001","272500006","278200081"]}},{$unset : {"asenodMigrationDetails.aseMigrationStatus" : ""}});
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 0 }
In interactive mode the mongo shell will display the results of the last command run.
In your example, the return value of load() is a boolean indicating the success of loading and evaluating JavaScript from the specified file.
If you want to output results from commands in test.js, you need to explicitly call print() or printjson().
Your test.js should instead look like:
var result = db.organizations.updateMany(
{"_id" : {
$in : ["120300007","160800001","175100009","193000031","201700008","235500001","237600001","24600001","272500006","278200081"]}},
{$unset : {"asenodMigrationDetails.aseMigrationStatus" : ""}}
)
printjson(result);
For more information see Write Scripts for the mongo shell in the MongoDB manual.

Creating views in mongodb

I'm following this tutorial in creating mongo views
https://www.percona.com/blog/2017/01/13/mongodb-3-4-views/
The issue here is when i run the command
db.createView('employee_names','employee', [{ $project : { _id : 0, "fullname" : {$concat : ["$FirstName", " ", "$LastName"]}}}])
I get an error saying createView is not a function
My mongo version is 3.4. What am i missing .
Views work in MongoDb since version 3.4, so instead of RoboMongo one might need Robo 3T. And of course this will work is shell as well. (Assuming old installations are upgraded to 3.4 as described at https://docs.mongodb.com/master/release-notes/3.4/#upgrade.)
//There are two collection state and City I would Like to create view on them
db.createView(
... "statedistrict", //View Name
... "States", //First Collection Name
... [
... {$lookup:{from:"Districts",localField:"StateId",foreignField:"StateId", as:"trial"}},
... {$project:{"_id":0,"StateId":1,"StateName":1,"trial.DistrictName":1}}
... ]
... )
//After Pressing Enter You Will get below ok message.
{ "ok" : 1 }
//Then pass the below command to see the result/record
db.statedist.find().pretty()
0 means - don't display
1 means - display
From 1st collection I need all to display except _id. So, only _id is kept 0
From 2nd collection I don't need many. So, I have used trial.not_required_field_name : 0 (you can use any name instead of trial)

My mongodb insert command functions but the collection is not visible

I am running mongodb 3.2 inside a docker container. I have created a database fine, and am using it, when I run the following commands:
> show collections
>
> db.designs.insert({"name": "phil"});
WriteResult({ "nInserted" : 1 })
> show collections
>
> db.designs.find({}):
{ "_id" : ObjectId("579208b15ee45148bdd30a28"), "name" : "phil" }
Any help is appreciated, thanks!
so it turns out that this is caused by an old mongo client:
https://jira.mongodb.org/browse/SERVER-16361

Mongodb: No *.system.namespaces

I'm trying to repair a database through the console.
It's a replicaset with 3 members, I'm trying in a secondary and I always get the same error. When I list the available collections inside the db it doesn't show any *.namespace collection:
root#web:~# mongo xxx.210.xxx.12:27017/admin -uXXXX -pXXXXXXX
MongoDB shell version: 2.4.5
connecting to: xxx.210.xxx.12:27017/admin
xxxxxSet:SECONDARY> use facebook
switched to db facebook
xxxxxSet:SECONDARY> db.repairDatabase();
{
"ok" : 0,
"errmsg" : "clone failed for facebook with error: namespace query failed facebook.system.namespaces"
}
xxxxxSet:SECONDARY> show collections
xxxnts
xxes
xxxes_beta
system.indexes
system.users
told
show collections hides some collections from view. You can run a direct find on the collection though:
> db.system.namespaces.find();
Which shows:
{ "name" : "test.fs.files" }
{ "name" : "test.system.indexes" }
{ "name" : "test.fs.files.$_id_" }
…
However, if yours is gone then try creating it:
db.createCollection( "facebook.system.namespaces" );

MongoDB: Can't Remove Documents That Run On Localhost

I can't figure out how to remove documents that I created while testing my app. From within the mongo shell I get this:
> db.carshare.remove();
> show dbs
carshare 0.203125GB
crushFlow 0.203125GB
local (empty)
test (empty)
> db.carshare.remove({});
> show dbs
carshare 0.203125GB
crushFlow 0.203125GB
local (empty)
test (empty)
I'm a beginner and must be missing something very obvious, help?
MongoDB has a hierarchy structure that is DB -> collection -> documents.
So, are you trying to remove a DB, a collection, or a document?
Assuming a DB 'foo', with a collection 'test', with two documents:
> db.test.find();
{ "_id" : ObjectId("4ef54ed6c143a725c52d7ff6"), "name" : "mongo" }
{ "_id" : ObjectId("4ef54eeec143a725c52d7ff7"), "name" : "bob" }
to remove a document:
> db.test.remove({'name':'bob'});
> db.test.find();
{ "_id" : ObjectId("4ef54ed6c143a725c52d7ff6"), "name" : "mongo" }
to remove a collection:
> db.test.drop();
true
> show collections;
system.indexes
to remove a DB:
db.dropDatabase();
{ "dropped" : "foo", "ok" : 1 }
The Mongo documentation is very good:
http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-Deleting
http://www.mongodb.org/display/DOCS/dbshell+Reference
"carshare" and "crushFlow" are the names of databases (which is the output you get from showdb). If you want to see collections to remove documents from type use dbname (test by default) and then type show collections.
The list of collections that appears can have their documents removed by doing db.collection.remove() where collection is the collection name and db is now set to the database you want via the use databasename command.
Good luck and cheers!