Aleksandr Kravets
Aleksandr Kravets

Reputation: 5947

Spring Data MongoDB: BigInteger to ObjectId conversion

I have a problem with update query using Spring Data MongoDB. I retrieve some object's _id as BigInteger value. Then I want to make following query:

Query query = new Query(Criteria.where("_id").is(id));
Update update = new Update();
update.set("version",version);
mongoOperations.updateFirst(query, update, Audit.class);

Query part fails to match any documents since id value passed to is() somehow must be converted to ObjectId. I can't find any documentation on this kind of conversion. Will appreciate any help.

p.s.: SpringData Mongodb version 1.2

Upvotes: 5

Views: 8242

Answers (5)

GaspardP
GaspardP

Reputation: 4832

You can convert a BigIngeter to ObjectId using the hex representation of the BigInteger. However, an ObjectId is supposed to be exactly 24 characters long, and parsing a shorter string will fail in Java. Thus it's better to ensure that the hex representation is 0-padded appropriately:

String hexString24 = StringUtils.leftPad(bigInteger.toString(16), 24, "0");
ObjectId convertedId = new ObjectId(hexString24);
Query query = new Query(Criteria.where("_id").is(convertedId));

Upvotes: 0

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

You can convert it also manually:

ObjectId convertedId = new ObjectId(bigInteger.toString(16));
Query query = new Query(Criteria.where("_id").is(convertedId));

Upvotes: 6

Ramana
Ramana

Reputation: 1

//get the converter from the mongoTemplate

MappingMongoConverter converter = (MappingMongoConverter)mongoTemplate.getConverter();

//get the conversion service from the mongo converter

ConversionService conversionService = converter.getConversionService();

//iterate the status list and get the each id to add the arraylist

for(Status status: statusList){

    ObjectId objectIdVal = conversionService.convert(status.getId(), ObjectId.class);

    **//here status.getId() returns the BigInteger**
    statusArrayList.add(objectIdVal);           
}

//get the users list whose status is active  and cancel

query.addCriteria(new Criteria().where("status.$id").in(statusArrayList));

List<User> usersList = mongoTemplate.find(query, User.class);

Upvotes: 0

Trevor Gowing
Trevor Gowing

Reputation: 2296

Alternatively you can add an 'id' field to your collection classes or potentially a base class and annotate it with org.springframework.data.annotation.Id, as below:

import org.springframework.data.annotation.Id;

public abstract class BaseDocument {

    @Id
    protected long id;

This will allow you to perform the queries of the form:

public boolean doesDocumentExist(Class clazz, long documentId) {
    Query queryCriteria = new Query(Criteria.where("id").is(documentId));
    return mongoTemplate.count(queryCriteria, clazz) == 1;
}

Annotating your own id field with '@Id' will store your id as the mongo objectId, therefore saving you from doing the conversion yourself.

Upvotes: 1

Mik378
Mik378

Reputation: 22191

You probably want to write a custom Spring converter BigInteger => ObjectId and ObjectId => BigInteger.

See the doc part here: http://static.springsource.org/spring-data/data-document/docs/current/reference/html/#d0e2670

------UPDATE------

It seems that this kind of converter already exists in the Spring-Data-MongoDB library: http://static.springsource.org/spring-data/data-document/docs/1.0.0.M1/api/org/springframework/data/document/mongodb/SimpleMongoConverter.ObjectIdToBigIntegerConverter.html

So you just have to specify it in your Spring configuration.

Upvotes: 1

Related Questions