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

  • Home
  • SEARCH
  • 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 8609873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:59:03+00:00 2026-06-12T03:59:03+00:00

I have user accounts, expiring by time. Time is stored in MongoDB as a

  • 0

I have user accounts, expiring by time. Time is stored in MongoDB as a long in milliseconds. Users are identified by _id. Here’s the schema:

{
    "_id": {
        "$oid": "506a1438be4f73c11c000002"
    },
    "email": "admin",
    "password": "admin",
    "first_name": "admin",
    "last_name": "admin",
    "country": "USA",
    "group": "admin",
    "expires": 1349129272918
}

To add +N days to user accounts, I use find() and then update () (dont know why, but findAndUpdate can’t get previous value):

app.post('/users/add', function(req,res){
    addDays(req.body.id, req.body.days, function(status){
        res.send(200);
    })
});

function addDays(id, days, callback) {
    var obj_id = BSON.ObjectID.createFromHexString(id);
    db.collection("users", function(error, collection) {
        collection.find({_id: obj_id}, function(err, user) {
            var oldexp = user.expires;
            if (oldexp < new Date().getTime() ) {
                var newexp = new Date().getTime() + (86400000*days);
            } else {
                var newexp = oldexp + (86400000*days);
            }
            collection.update({_id: obj_id}, {
                $set: {
                    "expires": parseInt(newexp)
                }
            }, function(err, result) {
                callback('ok');
            });
        });
    });
}

To launch update days function, I use JS on frontend:

  $(".addDays").live("click", function(){
    var that = $(this);
    var id = that.closest('.row').find('input[name=id]').val();
    alert(id);
    $.post('/users/add', {
        "id": id,
        "days": 10
      }, function(data){
          alert(data);
      });
  });

Var id is a string-decoded ObejctID (like “506a1438be4f73c11c000002″), for every user it’s stored in the nearest input(name=”id”).

After I try to add days, even I get OK, my collection becomes damaged. Does anyone know why? Thanks!

  • 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-12T03:59:04+00:00Added an answer on June 12, 2026 at 3:59 am

    Since you say you need to make different update depending on if the current value is greater than “now” or not, why not do it with these two updates:

    now = new Date().getTime();
    db.users.update({_id:obj_id, expires:{$gte:now}, {$inc:{expires:now+{86400000*days}})
    db.users.update({_id:obj_id, expires:{$lt:now}, {$set:{expires:now+86400000*days}})
    

    I think this is safer than the logic you have now.

    Note that the order of updates here matters to make sure only one of the two statements has effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem, I have user accounts created from java(jsp) interface which uses
So I have a table of user accounts (Users). There needs to be functionality
We have a simple database. The User table holds users. The Accounts table holds
I have uploaded a video to my site and there are 5 users(user accounts).
For several applications I made for my current client I have shared user accounts.
I have a User model and an Account model. The user has many accounts
Should I constantly have a session open in my web site with user accounts,
We have an older VB6 application that used to run under various user's accounts.
In my table accounts I have four columns like user, pass, column1, column2 I
I have 3 tables: users (id, name) currency (id, name) accounts (id, user_id, currency_id,

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.