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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:02:50+00:00 2026-06-10T20:02:50+00:00

I want to execute a java script file via the mongos for inserting data

  • 0

I want to execute a java script file via the mongos for inserting data to my sharding set. In addition to that I want to add a dynamic variable and the NULL value –

I would login (manually) to the shell by

mongo hostip:port/admin my_script.js

My js looks like:

var amount = 1000000;
var x=1;
var doc= ”;

for (i=0; i<amount; i++)
{

doc = { a: '1', b: '2', c: 'text' , d:  'x', e: 'NULL'}
db.mycol.insert(doc);
x=x + 1

}

(Rather “x” I could just use “i”)
does “d” writes the value of “x” or just the letter “x”?
does “e” writes the text “Null” or the .. let’s say “database NULL”
Is the way I do that procedure correctly? (Concerning how I connect to mongos / the sharding set)

best regards

EDIT:
And very important – how can I figure out the time, the mongodb/sharding set needs to store all the data? And balanced it?

Edit 2nd:

Hi Ross,

I have a sharding set that consists of two shards (two replicasets). At the moment I’m testing and therefore I use the loop-counter as the shard key.
Is there a way to check the time within the javascript?

Update:

So measuring the time that is needed for storing the data is equivalent to the time the javascript is executed? (Or the time the mongo shell isn’t accessible because of executing)

Is that assumption acceptable for measuring the query response time?
(where do I have to store the java script file?)

  • 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-10T20:02:52+00:00Added an answer on June 10, 2026 at 8:02 pm

    You dont need to keep multiple counters – as you are incrementing i on each iteration of the for loop. As you want the values and not strings the use i for the value of d and null instead of the string "NULL" – heres the cleaned up loop:

    var amount = 1000000;
    for (i=1; i<amount+1; i++) {
      doc = { a: '1', b: '2', c: 'text' , d:  i, e: null }
      db.mycol.insert(doc);
    }
    

    Regarding how long it takes to store / balance your data – that depends on a few factors.

    Firstly, what is your shard key? Is it a random value or is it an increasing value (like a timestamp). A random pattern for shard keys help ensure an even distribution of writes and if you know the ranges of the shard key, you could pre-split the shard to try and ensure that it stays balanced when loading data. If the shard key is increasing like a timestamp then most likely one shard will become hot and it will always be at the top end of the range and will have to split chunks and migrate the data to the other shards.

    At MongoDB UK there were a couple of good presentations about sharding: Overview of sharding and Sharding best practices.

    Update:

    Regarding how long will it take for the shards to become balanced – this depends on the load on your machines. Balancing is a lightweight process so should be considered a background operation. Its important to note, that even with a sharded system as soon as the data is written to the mongos its accessible for querying against. So if a shard becomes imbalanced during a data load the data is still accessible – it may take time to rebalance the shard – depending on the load of the shard and the additions of new data, meaning chunks need to be split before migrating.

    Update2

    The inserts to mongos are synchronous, so the time it takes to run the script is the time it took to apply the inserts. There are other options about the durability of writes using getLastError essentially how long you block while the write is written. The shell calls getLastError() transparently but the default for your language of choice is to be asynchronous and not wait for a server response.

    Where to store the javascript file? – Well thats up to you – its your application code. Most users will write an application in their preferred language and use the driver to call mongodb.

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

Sidebar

Related Questions

I want to execute an SQL script file in Java without reading the entire
I want to execute a batch file from a java program. I am using
I want to execute database import from .sql file from java program. My program
I want to load .php files via ajax which execute ExtJS script as they
I want to call a method in the action file from a java script
I want to execute Java Script generated from an MVC action method, I read
In my Java application, I want to run a batch file that calls scons
I have a shell script which I want to execute within a Java class.
I want to execute any JavaScript function that is part of a JSON using
i want to get ip server where my tomcat is execute in java (Struts).?

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.