2

I want to remove or delete one or more documents from a collection in MongoDB without using MongoDB Compass.

How can I remove or delete a document using Command Prompt ?

I have create a db 'University' and a collection 'names'. On the collection 'names' I have create 8 documents the 5 are full with data and the rest 3 are with data that I don't want them on my collection. How I can remove those 3 document?

5
  • There is detailed description in the docs on how to connect from shell(command prompt), use database to work upon, and ops on collection of that db docs. Is there something specific to that you are facing as problem. Commented Sep 18, 2019 at 15:39
  • I have create a db 'University' and a collection 'names'. On the collection 'names' I have create 8 documents the 5 are full with data and the 3 are with a data that I don't want them on my collection. How I can remove them ? Commented Sep 20, 2019 at 10:01
  • Is mongodb on your local system (localhost)??.. then just open command prompt 1. mongo --port 27017 to connect to shell 2. use University 3.show collections will show you collections on the db, if you see a names collection.? Commented Sep 20, 2019 at 10:14
  • Yes I have created the db University & Collections names via command prompt. Yes I can see the the collection names Commented Sep 27, 2019 at 5:33
  • Okay! So to remove those 3 documents from names collection, you know ids of those documents right. Do as one of the answers. db.names.deleteOne({_id: ObjectId('idofdoctodelete')}). That you run 3 times for those 3 docs. Commented Sep 27, 2019 at 7:10

1 Answer 1

2

Connect to mongo shell.

Run the following command

use db_name
db.getCollection(*collection_name*).deleteOne()

You can refer mongoDb doc as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.