Using upsert with the updateOne() method

I’m using upsert with the updateOne() method as bellow to perform an update.

db.practice.updateOne(
   {“title”:“Night Life”}, 
   {$set: detail}, 
   {upsert: true} 
)

My query returns the following:

{
        "acknowledged" : true,
        "matchedCount" : 0,
        "modifiedCount" : 0,
        "upsertedId" : ObjectId("5f8884fed29ded706c3c6737")
}

However, when I check my collection, I don’t see the document with the ObjectId("5f8884fed29ded706c3c6737

Below is the detail variable:
let detail = {
“title” : “Night Life”,
“year” : 2021,
“rated” : “PG-13”,
“released” : 2021,
“runtime” : 60,
“countries” : [
“USA”,
“UK”
],
“genres” : [
“comedy”,
“drama”
],
“director” : “Alpha Ly”,
“actors” : [
“Alpha Ly”,
“Kris Dasha”,
“Hope Grace”
]
}

NB: the number of documents in my collection is still the same as created.
What do you think is the problem?

You are looking in the wrong collection, wrong database or wrong cluster.

@steevej-1495
Even though I’m following the example in class, I’m applying it to my own database, collection.
The example in class can be applied to any database right?

Definitively.

Post screenshot of where and how you are looking for

ObjectId("5f8884fed29ded706c3c6737")

@steevej-1495
Everything you see is what’s on my screen. Nothing different.
If I understand you, the examples in class can’t be applied outside of class?
I don’t think so.

The is no find command for the specific document in what you shared. You are writing that you do not see the document. How did you look and where did you look?

All examples can be done any where as long as you have the right data. Since you are doing an upsert there is not prior data so you can do that anywhere.

@steevej-1495
When I look at the database in compass, it’s not there. And I can read “This collection has no data” in compass.
But when I use the find(), it returns the entry.

So the following is not completely true

If I understand correctly, in shell, with db.practice.find(…) you see the document. But in Compass you do not.

Post the following:

  1. from the shell the output of the command db
  2. from the shell the output of the command show dbs
  3. how you connect with the shell
  4. Compass window after you connect.

@Alpha_Ly Hello!

@steevej-1495 was trying to find out whether

  • the collection was under the right database.
  • compass was looking at that same place.

One problem is, update() creates the collection if it doesn’t exist.

This command:

db.practice.updateOne(
   {“title”:“Night Life”}, 
   {$set: detail}, 
   {upsert: true} 
)

Would succeed almost anywhere!


Hopefully you got that sorted anyways. But feel free to ask any extra doubts.
Hope you don’t mind I hop in :slight_smile:

1 Like

@Santiago_Miranda
@steevej-1495
You’re right about that.
I will check see what happen and will let you know.
Thanks

1 Like

@Santiago_Miranda
@steevej-1495
SteeveJ was right. My apologies.
It created the entry and I was looking at the wrong place.
My problem is solved now.
Thanks y’all

@Santiago_Miranda
@steevej-1495
Miranda, looking back, I can tell that SteeveJ knew what he was doing and I also can tell that he knows the stuff. BUT he assumed I understood what he was trying to accomplish.
You told me exactly what he was trying to accomplish and right away something clicked on my mind. Before I even went back to check, I already knew my mistake thanks to what you said…
So I thank you two very much for helping.
Thanks

1 Like

@Alpha_Ly No worries, things happen. Sorry if there were communication issues.

Have a good rest of the course, you can always come back for help :slight_smile:

1 Like

Closing this thread as the issue has been resolved. Please feel free to create a new thread if you have any other doubts.

~ Shubham