2

In my web project, I have older and newer buttons. If I click on older it has to display previous blog details n vice versa for newer. Now I am passing current id in the backend and I have to search and find previous id in MongoDB. How to get that. If any links related to this please provide.

This is piece of code where I am taking current id:

Controller.js:

$scope.previousBeat = function(beatId){
                            var beatId = beatId;
                            blogService.getPreviousBlog(beatId, function(data){
                                $scope.blogId = data;
                            })
                        };

In service.js:

this.getPreviousBlog = function(id, callback){
    var url = 'blog/' + id + '/details';
    httpService.getRequest(url, callback);
};

In controller.java:

@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
    public @ResponseBody Blog getPreviousBlogWithId(@PathVariable("id") String id) {
        try {
            Blog blog = blogService.findBlogById(id);

            return blogService.incrementViewCount(blog);
        } catch (Exception e) {
            // log.error("Error while retrieving blog", e.getMessage());
        }
        return null;
    }

Service.java:

 @Override
    public Blog findBlogById(String id) {
        Blog blog = blogRepo.findOne(id);
        find().sort({_id:1})

        if (blog != null) {
            return blog;
        }
        return null;
    }

Is there any way to get previous id.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Jayashree
  • 107
  • 19
  • 1
    Possible duplicate of [Next and previous documents](http://stackoverflow.com/questions/19123483/next-and-previous-documents) – Blakes Seven Nov 23 '15 at 07:48

1 Answers1

2

If your collection records are sorted by id then you can write like this:

db.blog.find({_id: {$lt: priviousId}}).sort({_id: 1}).limit(1)[0];

Basically, we are sorting by id and then using $lt to sort over _id in your collection and getting only the first record. That will be your previous Blog.

In the Groovy code:

blogRepo.find(["_id": ["$lt": priviousId]]).sort(["_id": 1]).limit(1)[0];
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • ok..Thank u..And priviousId is which pass form front end right? – Jayashree Nov 23 '15 at 07:21
  • Yup, `priviousId` is one you are passing from front-end. – Shashank Agrawal Nov 23 '15 at 07:23
  • Multiple markers at this line - Syntax error on tokens, delete these tokens - Syntax error, insert ")" to complete MethodInvocation - Syntax error, insert ")" to complete MethodInvocation - Syntax error on token ")", invalid Expression - Syntax error, insert ":: IdentifierOrNew" to complete ReferenceExpression - Syntax error, insert ";" to complete Statement - Syntax error, insert ";" to complete Statement - Syntax error, insert ";" to complete BlockStatements - Syntax error, insert "AssignmentOperator Expression" to .. – Jayashree Nov 23 '15 at 07:43
  • Blog blog =blogRepo.find({_id: {$lt: priviousId}}).sort({_id: 1}).limit(1)[0];..This line oly – Jayashree Nov 23 '15 at 08:47
  • What is `blogRepo` and which is your collection? I assumed that `blogRepo` is your collection. – Shashank Agrawal Nov 23 '15 at 08:59
  • blogRepo is only my Collection..i just assigned it blog variable so that return blog.. – Jayashree Nov 23 '15 at 09:43
  • @Override public Blog findPreviousBlogById(String priviousId) { Blog blog = blogRepo.findOne(priviousId); blog.find({_id: {$lt: priviousId}}).sort({_id: 1}).limit(1)[0]; //Blog blog =blogRepo.find({}).sort({_id:-1}).limit(1); if (blog != null) { return blog; } return null; } – Jayashree Nov 23 '15 at 09:43
  • Sorry, I posted the answer as the Groovy/Mongo shell code. Please port it to work in Java. I think, you have to use the `BasicDBObject` class. – Shashank Agrawal Nov 23 '15 at 10:12
  • @ShashankAgrawal `db.blog.find({_id: {$lt: priviousId}}).sort({_id: 1}).limit(1)[0];` will this query works? I think in `sort` `_id` would be `-1` to take the reverse order – gourav Jul 09 '19 at 04:18