user3742525
user3742525

Reputation: 15

mongo db throws error trying to edit _id field in update

I am trying to update a document

final Update update = new Update(); update.set("id", String.valueOf(partnerId)); update.pushAll("appIds", appIds.toArray()); mongoTemplate.upsert(Query.query(Criteria.where("partnerId").is(partnerId)), update, PartnerAppIds.class);

On executing this getting this error { "err" : "After applying the update to the document {_id: \"66\" , ...}, the (immutable) field '_id' was found to have been altered to _id: \"5}

The document looks like this

public class PartnerAppIds {
@Indexed
private String id;

@Indexed
private Long partnerId;

@Indexed
private Set<String> appIds;

public Long getPartnerId() {
    return partnerId;
}

public void setPartnerId(Long partnerId) {
    this.partnerId = partnerId;
}

public Set<String> getAppIds() {
    return appIds;
}

public void setAppIds(Set<String> appIds) {
    this.appIds = appIds;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

My question is as I have a id field, why mongo is assuming i am updating '_id' field, where as I am updating the id field here. Are _id and id same for mongo here

Upvotes: 1

Views: 1339

Answers (1)

s7vr
s7vr

Reputation: 75964

From the docs,

A property or field without an annotation but named id will be mapped to the _id field.

When querying and updating MongoTemplate will use the converter to handle conversions of the Query and Update objects that correspond to the above rules for saving documents so field names and types used in your queries will be able to match what is in your domain classes.

So use field with different name other than _id or id if you need to update.

Upvotes: 2

Related Questions