deleteOne not working

I’m looking at the last lecture in Chapter 2 and none of my delete commands are working. it’s acknowledging the command but not executing it…

here’s my command:
db.reviews.deleteOne({ _id: ObjectId(‘5be654a510007ca4cbb98294’)});

here’s the message I get:
{ “acknowledged” : true, “deletedCount” : 0 }

Double check the way you specified the value for _id. If you don’t see it, google the manual page for deleteOne, or research something like “MongoDB deleteOne objectid”.

I copied the _id directly from Compass, and tried setting it with both single and double quotes. I also compared it character by character with the script in the lesson, and it seems to match exactly, other than the fact that the value for _id was different. Is there something I’m missing?

If the _id values do not match … :thinking:

Start Simple — Double Check you are working in the same db & collection in Compass & shell

  • Use Compass as a Visual Aid and to Verify that the doc exists. { utilize the filter }
  • Use the mongo shell findOne() to search for the same document

If they are the same … then proceed with the deleteOne() action.

2 Likes

Ooh, good idea to try findOne() first.

I did already do all the checks to make sure I was looking at the same database, etc, and filtered in Compass, so that’s a great next step. Thanks!

1 Like

Thanks to your question I was forced to double-check the deleteOne() manual page.

https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/

Along the way I learned something new! :smiley: Initially I thought your syntax was off, but it wasn’t :slight_smile: So, thank you for making me check!

1 Like

Wayne, thank you! Doing a find() first worked.

1 Like

@Kirstin_95006 ,

You are Welcome. Glad to have Helped. :sunglasses:
Sometimes you have to step back, take a breath, and start simple.

The funny part is I spent my entire work day doing that :stuck_out_tongue_winking_eye:

(While actually using some of the things I learned in class!)

1 Like

I am having the same problem as Kirstin_95006.
Here is the confirmation
{
“acknowledged” : true,
“insertedIds” : [
ObjectId(“5c94dfb001349305909b1e86”),
ObjectId(“5c94dfb001349305909b1e87”),
ObjectId(“5c94dfb001349305909b1e88”),
ObjectId(“5c94dfb001349305909b1e89”),
ObjectId(“5c94dfb001349305909b1e8a”),
ObjectId(“5c94dfb001349305909b1e8b”),
ObjectId(“5c94dfb001349305909b1e8c”),
ObjectId(“5c94dfb001349305909b1e8d”),
ObjectId(“5c94dfb001349305909b1e8e”),
ObjectId(“5c94dfb001349305909b1e8f”),
ObjectId(“5c94dfb001349305909b1e90”),
ObjectId(“5c94dfb001349305909b1e91”),
ObjectId(“5c94dfb001349305909b1e92”),
ObjectId(“5c94dfb001349305909b1e93”),
ObjectId(“5c94dfb001349305909b1e94”),
ObjectId(“5c94dfb001349305909b1e95”),
ObjectId(“5c94dfb001349305909b1e96”),
ObjectId(“5c94dfb001349305909b1e97”),
ObjectId(“5c94dfb001349305909b1e98”),
ObjectId(“5c94dfb001349305909b1e99”)
]
}
I got after I pushed the documents. Then tried to delete the first item in the list with:
db.reviews.deleteOne({"_id": “ObjectId(‘5c94dfb001349305909b1e86’)”})
but no luck, I got the responce: { “acknowledged” : true, “deletedCount” : 0 }

By the way I even deleted the whole reviews database and added it back. Still no luck. Help please

Try the solution that Wayne offered: insert your records, then find the one you want to delete, then delete it. For some reason that made a difference for me.

I have tried it but no luck. It seems I could not select a document using “_id” selector both on the console and Compass app. I have tried to use “reviewer_id” and it works.
When I use the _id filter with the findOne method it returns null, and same thing on the Compass app too.

This is absolutely not working. I have gone through this video over and over, researched the docs, and I keep getting a ReferenceError every time I run the command shown on the video:

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.reviews.findOne(“5d218c370252d02c42e8f92b”)

{

“_id” : ObjectId(“5d218c370252d02c42e8f92b”),

“reviewer_id” : 275410481,

“rating” : 2.5,

“reviewer” : “Matthew Samuel”,

“text” : “There have been better movies made about space, and there are elements of the film that are borderline amateur, such as weak dialogue, an uneven tone, and film cliches.”

}

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.reviews.deleteOne({"_id" : ObjectID(“5d218c370252d02c42e8f92b”) } );

2019-07-06T23:26:12.871-0700 E QUERY [js] ReferenceError: ObjectID is not defined :

@(shell):1:23

MongoDB Enterprise Sandbox-shard-0:PRIMARY> db.reviews.deleteOne({"_id" : ObjectID ( “5d218c370252d02c42e8f92b”) } );

What am I doing wrong?

You need to pass the _id value as an ObjectID, not a string
This may help

Hi @Sarah_39095,

Please check your above query as this should return error.

Following is the correct way to pass _id field in the query filter:

db.reviews.findOne({"_id" : ObjectId(“5d2478b166c2bde3db6800f1”)})

Same is the format for deleting documents using _id field.

Also, try to avoid copy/paste the queries as it might sometimes give errors.

Please let me know, if you have any questions.

Thanks,
Sonali

I was having the same problem, then I figured out that I had mistakenly run use reviews
because the function is db.reviews it wont find a Database named reviews

then I ran use video, after that both find one and delete one worked.

1 Like

Hi @Soura_90090,

I’m glad your issue got resolved. If you have any query then please feel free to get back to us.

Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Hi everyone, I found a solution to this, according to an example found on the mongodb doc.

The trick is to actually assign your collection to a variable, then operate on that variable using the standard dot notation.

Mongodb seems to have trouble setting up the refs to different collections within the dbs, this has happen to me on multiple occasions while going through the lessons today.

What I did is this on the mongodb shell:

coll = db.reviews
coll.deleteOne({_id: ObjectId(“5dc8bc8173b4bb46267612b1”)})
coll.deleteMany({reviewer_id: 759723314})

MongoDB Enterprise Cluster0-shard-0:PRIMARY> coll.find({rating : 5}).count()
3
MongoDB Enterprise Cluster0-shard-0:PRIMARY> coll.deleteMany({rating:5})
{ “acknowledged” : true, “deletedCount” : 3 }
MongoDB Enterprise Cluster0-shard-0:PRIMARY> coll.find({rating : 5}).count()
0

Also typos and letter capitalization are very important in MongoDB, it cares about those little details!

Hi @harvey_lin_85455,

Yes, this is one of the ways in which you can work with your database and collection in the mongo shell.

Yes, just like any other modern programming language mongo shell is case sensitive. It is basically a javascript interpreter.

Can you please tell me more about the problem that you were facing while trying to reference different collections in a database ?

Thanks,
Shubham Ranjan
Curriculum Support Engineer