Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:11:24+00:00 2026-06-11T14:11:24+00:00

I have created a ‘sequence’ collection. It has ‘name’ and ‘counter’ fields. The ‘name’

  • 0

I have created a ‘sequence’ collection. It has ‘name’ and ‘counter’ fields. The ‘name’ field is set to ‘Tag’ and ‘counter’ field is set to 0. Following is the code within a method that returns the next value of ‘counter’ field.

final Sequence tagSequence = mongoTemplate.findAndModify(
  new Query(Criteria.where("name").is("Tag")), 
  new Update().inc("counter", 1), 
  new FindAndModifyOptions().returnNew(true), Sequence.class);

However, this piece of code does not work. Any inputs?

I found an implementation for this here, http://dev.bubblemix.net/blog/2011/04/auto-increment-for-mongodb-with-the-java-driver/, however, I wanted to know what is wrong with this piece of code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:11:25+00:00Added an answer on June 11, 2026 at 2:11 pm

    Your code works correctly provided that you have first saved the Sequence object, with the “Tag” name, in MongoDB. I’d suggest you annotate the name field with the @Id annotation. Ex.:

    public class Sequence {
        @Id
        protected final String name;
        protected Long counter;
    
        public Sequence(String name, Long counter) {
            this.name = name;
            this.counter = counter;
        }
        public String getName() {
            return name;
        }
        public Long getCounter() {
            return counter;
        }
        public void setCounter(Long counter) {
            this.counter = counter;
        }
    
    }
    

    Save the object in MongoDB:

    template.save(new Sequence("Tag",0L))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created an EDMX in visual studio 2010 SP1. It has been built
Have created simple Ajax enabled contact forms before that have around 12 fields -
I have created a tab-pane using HAML as shown in my code snippet below
Have created a custom navigation menu in wordpress that has some pages and some
I have created RadioGroup and RadioButton dynamically as following: RadioGroup radioGroup = new RadioGroup(context);
I have created an XML file in my solution path. My solution has multiple
I have created a set of tables by executing SQL script generated by ADO.NET
I have created a UserControl that has a ListView in it. The ListView is
Have created an app-id as normal. Then created an app-store distribution profile...as normal. Set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.