My mongodb insert command functions but the collection is not visible - mongodb

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

Related

mongo shell ReferenceError: database_name not found

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!!

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.

Can not create empty collection in MongoDB using Meteor

I have the next code in lib/ folder
TestCollection = new Mongo.Collection('testCollection');
I expect that this code should create empty collection in MongoDB,but it doesn't.
This collection is created only when I insert some data.
TestCollection = new Mongo.Collection('testCollection');
TestCollection .insert({ foo: 'blah', bar: 'bleh' });
I want to create an empty collection without simultaneously inserting data. Is it possible?
I looked at similar posts,but they insert data immediately after the creation collection. I want first create collection and much later insert data.
From Meteor, no. You can create an empty collection from the mongo shell using db.createCollection
Otherwise just insert a doc and remove it right away as suggested by #CodeChimp
same as Michel Floyd say, I have done this in mongodb shell:
> use storybook
> db.main.insert({"fakeKey": "fakeValue"})
WriteResult({ "nInserted" : 1 })
> db.main.find()
{ "_id" : ObjectId("5bd2bddb6ec5fdeabfbd6ecb"), "fakeKey" : "fakeValue" }
> db.main.deleteOne({"_id": ObjectId("5bd2bddb6ec5fdeabfbd6ecb")})
{ "acknowledged" : true, "deletedCount" : 1 }
then via:
db.main.find()
you can find that already added an empty db's collection.

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!