Search code examples
mongodbsmalltalkpharo

Embedded root object stored with wrong id


I have an Object called Session where another root-object called User is declared as owner. In MongoDB the owner field is indexed as unique: in Session>>ofUser: I send the following message:

ofUser: anASUser
    ^ (self selectOne: [ :each |
        (each at: 'owner.__id') = anASUser voyageId ])
    ifNil: [ self new
        owner: anASUser;
        save ]

This is the User in the user DB:

{"_id":"5b5e006643d001c78f2e88d6",
    "#instanceOf":"ASUser",
    "#version":"-551686239533400057",
    "name":"zack"}

This is the value of self voyageId of the ASUser in the debugger:

OID(5B5E006643D001C78F2E88D6)

And this is the saved AKMSession object from the ofUser method:

{"_id":"5b5ef4350f2532682e2e9536",
    "#instanceOf":"AKMSession",
    "#version":"-928582753905278919",
    "action":null,
    "owner":
       {"collection":"ASUser",
           "#instanceOf":"ASUser",
           "__id":"5b5ecf7a0f2532d63a2e952f"},
       "recipe":null}

Why is the owner.__id of the AKMSession object differing from the ASUser Id?

Just to summarize:

  • The first time of execution, when there is no Session Object for the specific user, the ifNil: Block is executed and the Session object with an ASUser as owner is created and saved into MongoDB
  • The owner __id differs from the OID of the ASUser.
  • the second time of execution the the selectOne: block still does not find the Session object with the specific user.
  • So either the "self new, save block" is wrong, or I have a wrong select query.

Solution

  • OK this is my bad. I mixed MongoDBs and the method was permanently creating ASUsers in the wrong DB as a different collection (wasn't indexed, so it could always create a new entry).

    So if you access one db for users and store objects with an embedded User in another db be aware, that this can happen.

    This is happening, although the ASUser Class has the ASUser>>voyageRepository returning the correct DB.