简体   繁体   中英

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM