0

I'm having a problem with inserting test documents into my MongoDB.

I'm using JavaScripts to create my test documents and then running them via the MongoDB Shell. But I'm getting some unexpected results.

My script creates a document with the following tree structure.

-Project
  -Task
    -Timesheet

When I try to create 5 Projects with 15 Tasks and 50 Timesheets (createCollections("my project", 5, "my task", 15, 50, 100);) I'm only getting 2 complete (error free) documents in the database. When I try to create 5 Projects with 1 Tasks and 5 Timesheets (createCollections("my project", 5, "my task", 1, 5, 100);) I'm only getting 4 complete (error free) documents in the database. Rather odd.

I'm sure the scripts runs through the expected iterations as I placed prints to verify that the insert was actually run.

Output: (As expected.)

before 0
after 0
before 1
after 1
before 2
after 2
before 3
after 3
before 4
after 4

The script

db.test_embedded.drop();
db.createCollection("test_embedded");

var projectKey = 0;
var taskKey = 0;
var timesheetKey = 0;

Array.prototype.randomElement = function () {
    return this[Math.floor(Math.random() * this.length)]
}

function Iterator(array){
    var i = 0;
    this.array = array;
    this.length = array.length;

    this.next = next;
    function next() {
        return this.array[i++ % this.length];
    }
}

var userIds = new Array();
db.users.find({},{_id:1}).forEach(function (user) { userIds.push(user._id) });

var projectCollaboratorSlice = 0;
function project(title) {
    this._id = "project" + projectKey++;
    this.title = title;
    this.owner = userIds[0];
    this.collaboraters = userIds.slice(projectCollaboratorSlice++ % userIds.length + 1);
    this.tasks = new Array();
}

var taskCollaboratorSlice = 0;
function task(title) {
    this._id = "task" + taskKey++;
    this.title = title;
    this.owner = userIds[0];
    this.collaboraters = userIds.slice(taskCollaboratorSlice++ % userIds.length + 1);
    this.timesheets = new Array();
}

var timesheetUserIter = new Iterator(userIds);
function timesheet(duration) {
    this._id = "timesheet" + timesheetKey++;
    this.owner = timesheetUserIter.next();
    this.date = new Date(0);
    this.duration = duration;
}

function createTimesheets(num, duration) {
    var timesheets = new Array();
    for (k = 0; k < num; k++) {
        timesheets.push(new timesheet(duration));
    }
    return timesheets;
}

function createDoc(projectName, taskName, numTasks, numTimesheets, duration) {
    var newProject = new project(projectName);
    for (var i=0; i<numTasks; i++) {
        var newTask = new task(taskName + i);
        newTask.timesheets.push(createTimesheets(numTimesheets, duration));

        newProject.tasks.push(newTask);
    }

    return newProject;
}

function createCollections(projectName, projectCount, taskName, taskCount, timesheetCount, timesheetDuration){
    for (var p=0; p<projectCount; p++) {
        print("before" + p);
        db.test_embedded.insert(createDoc(projectName + p, taskName, taskCount, timesheetCount, timesheetDuration));
        print("after" + p);
    }
}
createCollections("my project", 5, "my task", 1, 5, 100);

Is it my script that's the cause or is there a restriction/bug on how much you can do via scripts?

Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
  • I've noticed unusual behavior with more complex scripts in the shell, and I've always switched to another programming language/environment to complete more complex tasks (such as NodeJs for example). – WiredPrairie Apr 14 '13 at 20:51
  • I see, I'll get it a try – Snæbjørn Apr 14 '13 at 20:54
  • What version of MongoDB server/shell are you using, and on what O/S? – Stennie Apr 15 '13 at 21:02
  • I saved your JS and tried running via command line with MongoDB 2.4.1 (OS X) and it seems to work OK for both examples you've provided (eg `mongo testlimit.js`, then checking the count of tasks and timesheets in the test_embedded collection). How are you verifying that the docs are "complete"? – Stennie Apr 16 '13 at 03:08
  • @Stennie I'm running Win7 and MongoDB Shell v2.4.1, I'm using MongoHQ.com as my server so whatever version they're using. But I'm able to run the script just fine now. Perhaps MongoHQ was throttling me somehow :/ – Snæbjørn Apr 16 '13 at 14:09
  • I retested with Windows and 2.4.1 shell and still didn't have the issue. There is a possible bug in the Windows 2.4.1 `mongo` shell (see [SERVER-9294](https://jira.mongodb.org/browse/SERVER-9294), but I wasn't able to reproduce with your test script. If you have a reproducible test script can you please attach/comment on that SERVER issue. – Stennie Apr 16 '13 at 20:53

0 Answers0