In this tutorial, we will show you how to generate an auto-incrementing sequence id in MongoDB + Spring Data environment.
Tools used in this project :
Spring Data MongoDB 1.2.1.RELEASE
MongoDB 2.4.5
Eclipse 4.2
Maven 3
At the end of this tutorial, if the collection name
“hosting” is saved, a new auto-incrementing sequence id will be
assigned. Below is the Java code snippet to generate the sequence id.
public long getNextSequenceId(String key) {
Query query = new Query(Criteria.where("_id").is(key));
Update update = new Update();
update.inc("seq", 1);
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
SequenceId seqId =
mongoOperation.findAndModify(query, update, options, SequenceId.class);
return seqId.getSeq();
}
1. Project Structure
Review the project directory structure, a standard Maven project.
2. Maven Pom
In case you are interested at the project dependencies.
We create a collection name “sequence” to store the auto increase sequence id. Refer to the SequenceDaoImpl.java below, it shows you the code to generate the sequence id.
Note
Create the “sequence” collection in your MongoDB first!
db.sequence.insert({_id: "hosting",seq: 0})
SequenceId.java
package com.mkyong.seq.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "sequence")
public class SequenceId {
@Id
private String id;
private long seq;
//get, set, toString...
}
SequenceDao.java
package com.mkyong.seq.dao;
import com.mkyong.seq.exception.SequenceException;
public interface SequenceDao {
long getNextSequenceId(String key) throws SequenceException;
}
SequenceDaoImpl.java
package com.mkyong.seq.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.mkyong.seq.exception.SequenceException;
import com.mkyong.seq.model.SequenceId;
@Repository
public class SequenceDaoImpl implements SequenceDao {
@Autowired
private MongoOperations mongoOperation;
@Override
public long getNextSequenceId(String key) throws SequenceException {
//get sequence id
Query query = new Query(Criteria.where("_id").is(key));
//increase sequence id by 1
Update update = new Update();
update.inc("seq", 1);
//return new increased id
FindAndModifyOptions options = new FindAndModifyOptions();
options.returnNew(true);
//this is the magic happened.
SequenceId seqId =
mongoOperation.findAndModify(query, update, options, SequenceId.class);
//if no id, throws SequenceException
//optional, just a way to tell user when the sequence id is failed to generate.
if (seqId == null) {
throw new SequenceException("Unable to get sequence id for key : " + key);
}
return seqId.getSeq();
}
}
SequenceException.java
package com.mkyong.seq.exception;
public class SequenceException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String errCode;
private String errMsg;
//get, set...
public SequenceException(String errMsg) {
this.errMsg = errMsg;
}
}
4. Get the Sequence ID
To get the sequence id, uses sequenceDao.getNextSequenceId("key").
Comments
Post a Comment