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?