tags:

views:

1269

answers:

4

If I were building a blog I could use the blog title as the unique identifier and parse it through the URL. However, what if I wanted to use numbers. You know how twitter has www.twitter.com/username/statuses/9834542? Has anyone figured out a nice way of making this work? using "_id" is out of the question since it's way too long.

+4  A: 

As long as you can guarantee uniqueness, you're not constrained to using the default "_id" MongoDB supplies.

Therefore, it's down to you how you generate this number. If you'd like to store this number inside MongoDB, then you could store it in a separate collection and increment it for every new URL required.

Incrementing a field is achieved by using the $inc verb, or you may want to take a look at how MongoDB can atomically update or increment a value.

Alan
As Alan said, you can provide your own id. So the question is how you can generate it uniquely. Easiest is if you had some sequence server (i.e. something that dishes out a number and then increments, holding a lock so taht it happens atomically. This sequence server could use a single mongo record per sequence
Arne Claassen
+1  A: 

If you want to add a uniqueness constraint to your own field in MongoDB, use an index. Then you can use any hashing algorithm you want to generate the number and test it for uniqueness. The example in the MongoDB documentation is

db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

which will prevent you from inserting documents with the same firstname AND lastname as another document.

More information is available in the documentation.

Van Nguyen
A: 

I have solved this problem by creating collection 'sequence' with data:

  • name
  • currurt value

I'm using Morhpia, so have DAO for it. But you can do it without Morhpia too. Idea is to use $atomic (probably it can be omitted due updateing 1 instance only) and $inc modifier operator.

Sequence

@Entity(value = "sys_sequence", noClassnameStored = true)
public class SequenceM {

    /**
     * Names of entity
     */
    public static enum Entity {
        USER,
        CAPABILITY_HISTORY;

        public String getEntityName() {
            return this.name().toLowerCase();
        }
    }

    @Id
    private ObjectId uid;

    @Property
    @Indexed(unique = true)
    private String name;

    @Property
    private Long value;

 //..getters/setters/etc
 }

Method on SequenceDAO:

@NotNull
public Long nextValue(final @NotNull SequenceM.Entity entity) {
    final DB db = this.ds.getDB();
    final WriteConcern writeConcern = getWriteConcern();

    //optimization for JVM instance
    synchronized(entity) {
        do {
            SequenceM sequence = findOne("name", entity.getEntityName());

            final DBObject q = BasicDBObjectBuilder.start().add("name", entity.getEntityName()).add("value", sequence.getValue()).add("$atomic", 1).get();
            final DBObject o = BasicDBObjectBuilder.start().add("$inc", BasicDBObjectBuilder.start().add("value", 1).get()).get();

            WriteResult writeResult = db.getCollection("sys_sequence").update(q, o, false, true, writeConcern);

            if(writeResult.getN() == 1) {
                return sequence.getValue() + 1;
            }
        } while(true);
    }
}

/**
 * Determining writing concern basing on configuration
 */
private WriteConcern getWriteConcern() {
    return isOneNodeOnly ? WriteConcern.SAFE : REPLICATION_SAFE;
}

Depending on MongoDB configuration (one node only or master/slave or replica set) you have to use correct WriteConcern. Using REPLICATION_SAFE in one environment with one instance only causes infinite loop.

vmorarian
A: 

It can be done by using the findandmodify command.

Let's consider we have a special collection named sequences and we want to have a sequence for post numbers (named postid), you could use code similar to this:

> db.runCommand( { "findandmodify" : "sequences",
                   "query" : { "name" : "postid"},
                   "update" : { $inc : { "id" : 1 }},
                   "new" : true } );

This command will return atomically the updated (new) document together with status. The value field contains the returned document if the command completed successfully.

Hubert Kario