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 9222917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:56:57+00:00 2026-06-18T03:56:57+00:00

I am able to run the following update command from command line: db.Camera.update({_id:ObjectId(51059ca49c7b280809a2a81e)}, {$set:

  • 0

I am able to run the following update command from command line:

db.Camera.update({_id:ObjectId("51059ca49c7b280809a2a81e")}, {$set: {"Selected":0}});

But the following code in NodeJS doesnt work. When I log item.Selected, I get a non-zero value.

exports.findById = function(req, res) {
    var ids = req.params.id.split(",");
    db.collection('Camera', function(err, collection) {
        for(var i = 0; i < ids.length; i++){
            collection.findOne({'_id':idArray[i]}, function(err, item) {
                item.Selected += 1;
                console.log('Selected: ' + item.Selected);
                collection.update({'_id':new BSON.ObjectID(ids[i])}, item,function(err, updated) {
                  if( err || !updated ) console.log("Not updated");
                  else console.log("Updated");
                });
            });
        }
    });

};

In the console, I always see “Not updated”.

Thanks for your help.

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

    The id inside the loop isn’t what you expect because of your use of the variable i. These are callbacks, inside a closure. So, when the findOne returns, the loop has already finished, meaning it’s really not what you expect as i will be equal to ids.length, for every “Camera.”

    You can just use the item’s _id property directly when your code calls update (or anything else you’d like, just don’t rely on the loop index, unless you wrap the code within the loop all with a closure which likely isn’t necessary).

    Something like this … the query parameter to update call is what changed:

    {'_id':item._id)}
    

    Full code:

    exports.findById = function(req, res) {
        var ids = req.params.id.split(",");
        db.collection('Camera', function(err, collection) {
            for(var i = 0; i < ids.length; i++){
                collection.findOne({'_id':ids[i]}, function(err, item) {
                    item.Selected += 1;
                    console.log('Selected: ' + item.Selected);
                    collection.update({'_id':item._id)}, item,function(err, updated) {
                      if( err || !updated ) console.log("Not updated");
                      else console.log("Updated");
                    });
                });
            }
        });
    

    };

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

Sidebar

Related Questions

I want to be able to run Junit tests from the command line, but
I want to be able to run the following command using maven mvn tomcat7:deploy
I'd like to be able to run a command that opens up a git
Take the following demo code (from the GIO answer to this question), which uses
I am able to run a Linux command via RunTime class. Is there a
I am able to create able to run my migrations from JRuby 1.6.3 and
My project has no errors, I'm able to run it via F5, but I
Sometimes I want to be able to pull from a repo (via clone). But
I need to be able to run all of the following within one query
I have been able to run my C# .net 3.5 app just fine on

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.