0

I want to create a migration script that able to copy from Project assignedTo field to Project assignedMultiple but inserted in an array with the users detail on it and assignedTo field will be null. assignedTo field will be deprecated since we were gonna use assignedMultiple.

This is my users table:

User:

{
 "username" : "usertest",
 "firstName" : "Stack",
 "lastName" : "Overflow",
 "company" : "",
 "country" : "",
 "id" : ObjectId("this_is_usertest_id")
}

Project:

{
 "name" : "Project",
 "assignedTo" : ObjectId("this_is_usertest_id"),
 "assignedMultiple" : [],
 "id" : ObjectId("someid")
}

Expected Output:

{
 "name" : "Project",
 "assignedTo" : null,
 "assignedMultiple" : [{
     "username" : "usertest",
     "firstName" : "Stack",
     "lastName" : "Overflow",
     "company" : "",
     "country" : "",
     "id": ObjectId("this_is_usertest_id")
 }]
 "id" : ObjectId("someid")
}

This is what I have tried so far but nothing happens and giving me an error: failed to execute a script. TypeError: db.user.findOne(...) is null but when I try to use print(project) it prints the projects with data. This one is based on this link MongoDB : how to set a new field equal to the value of another field, for every document in a collection

db.project
.find().forEach(function(project)
{    
    if (project.assignedTo) {
        db.user.findOne({ where: { id: project.assignedTo } })
        .forEach(function(user) {
            var userObj = {
                "username" : user.username,
                "firstName" : user.firstName,
                "lastName" : user.lastName,
                "company" : user.company,
                "country" : user.country,
                "id" : user.id
            };
            db.getCollection('project').update( 
                {"_id" : project.id}, {$addToSet : {assignedMultiple : [...project.assignedMultiple, { "_id": project.assignedTo }]}}
            )
        });
    }
});

Thank you in advance!

the_lorem_ipsum_guy
  • 452
  • 1
  • 12
  • 27
  • 1
    It's not based on a correct answer though. You have syntax errors. Plenty of examples there without syntax errors. Hint look for `bulkWrite()` within the page. – Neil Lunn Mar 21 '19 at 09:06
  • @NeilLunn yeap, that is also one of the reasons why I ask some help since I was stuck and I don't have any idea why is `project.assignedTo` is giving me a null when I tried to print before the if statement is returning some data from the project. :) – the_lorem_ipsum_guy Mar 21 '19 at 09:13
  • Your errors however are from things like `db.user.findOne({ where:` which is actually not valid. MongoDB does not have a `where`. You simply issue the fields and values you want to match ( for equality; use other operators for other things ). If you have two collections, you either "join" via `$lookup` or "iterate" though both collections. Also extremely well covered in existing answers. – Neil Lunn Mar 21 '19 at 09:16
  • No worries. I'll just check into it. I'm still learning in mongo so I don't have any idea about joining two collections yet. Thanks a lot @NeilLunn – the_lorem_ipsum_guy Mar 21 '19 at 09:19

0 Answers0