0

I am having a problem with MongoDB ID changing after I upsert a document.

public function upsert(User $user) {
    $userMongo = new UserMongo($user);

    if(!$userMongo->getUuid()) {
      $userMongo->setUuid(new ObjectID);
      $this->collection->insertOne($userMongo);

    } else {
      $userMongo->setUuid(new ObjectID($userMongo->getUuid()));
      $this->collection->updateOne(["_id" => $userMongo->getUuid()], ["\$set" => $userMongo]);
    }

    $userMongo->setUuid($userMongo->getUuid()->__toString());
    return $userMongo;
  }

This should create a new document from php object with newly created objectID, and return the php object with created ObjectID. However this returns ObjectID A(ie. "5986c0c8e13823329a682c81") and stores it as ObjectID B(ie. "5986c0c8e13823329a682c82"). The stored ObjectID and returned ObjectID are slightly different (last digit).

Why am I getting different ObjectIDs and how can I retrieve the ObjectID of newly created document from upsert?

John Doe
  • 60
  • 2
  • 8
  • 1
    The `_id` field cannot possibly change, since it's a Primary Key and that is not allowed. Which property are you talking about? Because if a different property is being changed then it's **your code** that is doing it. But `_id` **cannot change**. – Neil Lunn Aug 06 '17 at 07:04
  • 1
    I just ran test to be sure. When I do a upsert, it returns ID "5986c0c8e13823329a682c81", when I do a findAll, that same document has different ID, "5986c0c8e13823329a682c82".. which is slightly different by 1 character. I am positive it is changing an I am not manipulating the ID either. – John Doe Aug 06 '17 at 07:27
  • The only reason you get a "new id" with **upsert** is because you **created a new document** instead of **updating the existing one**. Which means you likely have a failing in your logic. But we don't know what that is, because that is not the question you asked. Perhaps you should think a bit more before essentially posting a *"Is this a bug?"* question. Because it almost never is a bug, and it's you who are doing it wrong. Always **ask** by **showing what you are doing**. Then we can show you how to do it correctly. – Neil Lunn Aug 06 '17 at 07:30
  • I updated the question to show what I am doing and added some more information for better understanding, thank you for the suggestion. However, I was not asking if this is a bug. I am very new to Mongo and do not know 100% of its working yet or if this is normal behavior. – John Doe Aug 06 '17 at 07:44
  • Point was to "show your code" since that's what gets you real answers. So just briefly looking at this, it is not an **upsert**, but just your own function called upsert. Also `UserMongo` is again "your own code", in which you do not show what you are actually doing. So you should really make your code listing here "more complete". See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) in the help center here. Basically you need to show "enough code for someone else to replicate the problem". – Neil Lunn Aug 06 '17 at 07:48
  • 1
    Also read the ["upsert" section and examples](https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-option) in the core documentation, in order to see what you really should be doing here instead. – Neil Lunn Aug 06 '17 at 07:50

0 Answers0