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!
Related
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!!
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.
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
I just started learning mongoDB and I noticed a collection which is removed by .remove() command still exist after the executing.
am I doing something wrong or this is how it's supposed to work?
by using mongo
use testDB
db.stats() // returns "db" : "testDB","collections" : 0,"objects" : 0
//and db.getCollectionNames() returns nothing as well
db.testCollection.insert({ test : 'abc'})
db.getCollectionNames() // [ "system.indexes", "testCollection" ]
db.testCollection.remove()
db.testCollection.find() // returns nothing
db.getCollectionNames() // [ "system.indexes", "testCollection" ]
db.stats() // "db" : "testDB","collections" : 3,"objects" : 4
You missed the point of remove operation in mongodb. It does not remove the collection, it removes all documents in the collection which specify the query. If you do remove() you specify nothing in your query, thus it removes everything.
To remove collection do db.collection.drop()
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" );