Question

Ok to make this simple, here's the stripped down Morphia model:

@Entity
public class Person implements Serializable {


    @Id
    private ObjectId id;

    @Reference
    private List<Friend> friends = new ArrayList<Friend>();

    ...
}



@Entity
public class Friend implements Serializable {

    @Id
    private ObjectId id;

    private ObjectId personId;

    ...
}

A Person object contains a collection of Friend objects. These Friend objects contain a "personId" ObjectId, which is the ObjectId of a Person (A friend is also a person). Yes I know I could use a Person object here... but I won't get into why I did it this way.

Anyway, I use a query to get the actual Person object from the personId in the Friend Object:

final Person person = datastore.find(Person.class).field("_id").equal(personId).get();

This was working flawlessly until last night. For some reason the query started to return null (could not find the Person object based on the _id value).

I made no changes to the code up to this point. I didn't look into it, I just deleted my DB and started over, so my question here is very high level.

Can MongoDB arbitrarily alter the _id for a document? The personId ObjectId stored in the Friend object appeared to no longer point to a valid Person Object in the Person collection. Is this possible (which it appears to be)? I thought once a Document was created, there was no way to change the _id.

BTW, nothing special with the DB (i.e. no Sharding).

Était-ce utile?

La solution

The _id field is immutable, it cannot be changed.

I suspect that you had something else going on with your data or your test. You may have had a new Person record inserted with a new _id. If it happens again you may want to verify the contents of your collection using the mongo shell.

Autres conseils

No. _id fields are immutable in the database.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top