0

I'm using MongoDB on ubuntu server. I would like to generate my documents in and store them in an array. After that I would like to insert these documents into my collection, but somehow the collection is always empty. It's important, that the data generation is seperated from the storage for measuring. I'm using javascript for that. However, that's some code:

for (i=0; i<amount; i++)
{                                           
doc = "datetime:" + Math.floor((1262300400+Math.random()%(1356994799-1262300400+1))) + sourceport: " + Math.floor((Math.random()*30000)+2000) ;
myarray[i]=doc;
}

...

for (n=0;n<=myarray.length-1;n++)
    {
        obj_doc = eval('{' + myarray[n] +'}');
        eval('var obj='+myarray[n]);
        obj_doc = '{' + myarray[n] +'}';
        db.mycol.insert(obj_doc);
    }

It doesn't even work without the rnd() function. db.mycol.stats() always returns "count:0"

1 Answer 1

1

Build up the docs to insert as objects, not strings and eval:

for (i=0; i<amount; i++) {                                           
    doc = {
        datetime: Math.floor((1262300400+Math.random()%(1356994799-1262300400+1))),
        sourceport: Math.floor((Math.random()*30000)+2000)
    };
    myarray[i]=doc;
}

...

db.mycol.insert(myarray);
Sign up to request clarification or add additional context in comments.

7 Comments

No error occurs, but there are still 0 elements in the collection
@Tyzak What's amount set to when you run this? I tried this in the mongo shell with amount = 50 and it did insert 50 docs.
hm my amount is 100 ... what is about strings? I added a string like mystring: "test" is that okay?
@Tyzak Sorry, I don't understand your question.
hm sorry, I tried it with the code above and with an additional item like test: "test" as a string. Both don't work somehow - the count of my collection stats is always:0 ... I don't understand that :<
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.