laravel-mongodb:如何将外键添加到两个现有集合中?

I am using laravel-mongodb(https://github.com/jenssegers/laravel-mongodb) to manipulate data in mongodb,

I have two collections in mongoDB,they are books and categories.

later I will import them into mysql,so I want to correlate them with a foreign key first.

getting them from mongodb:

public function getBooksAndCategories()
{
    $books = DB::connection('mongodb')->collection('books')->get();
    $categories = DB::connection('mongodb')->collection('categories')->get();
}

they look like this:

    $books = collect([
        ['_id' => 'xxx','name' => 'history book 1', 'category' => 'history'],
        ['_id' => 'xxx','name' => 'history book 2', 'category' => 'history'],
        ['_id' => 'xxx','name' => 'science book 1', 'category' => 'science'],
        ['_id' => 'xxx','name' => 'science book 2', 'category' => 'science'],
        ['_id' => 'xxx','name' => 'sociology book 1', 'category' => 'sociology'],
        ['_id' => 'xxx','name' => 'sociology book 2', 'category' => 'sociology'],
        ['_id' => 'xxx','name' => 'economics book 1', 'category' => 'economics'],
    ]);




    $categories = collect([
        ['_id' => 'xxx','category' => 'history'],
        ['_id' => 'xxx','category' => 'science'],
        ['_id' => 'xxx','category' => 'sociology'],
        ['_id' => 'xxx','category' => 'economics'],
    ]);

first,i need to add a id into $categories,the result is like this:

    $newCategories = collect([
        ['_id' => 'xxx','category' => 'history','id' => 1],
        ['_id' => 'xxx','category' => 'science','id' => 2],
        ['_id' => 'xxx','category' => 'sociology','id' => 3],
        ['_id' => 'xxx','category' => 'economics','id' => 4],
    ]);

second, I need to add a foreign key into collection $books,the result is like this:

    $newBooks = collect([
        ['_id' => 'xxx','name' => 'history book 1', 'category' => 'history','category_id' => 1],
        ['_id' => 'xxx','name' => 'history book 2', 'category' => 'history','category_id' => 1],
        ['_id' => 'xxx','name' => 'science book 1', 'category' => 'science','category_id' => 2],
        ['_id' => 'xxx','name' => 'science book 2', 'category' => 'science','category_id' => 2],
        ['_id' => 'xxx','name' => 'sociology book 1', 'category' => 'sociology','category_id' => 3],
        ['_id' => 'xxx','name' => 'sociology book 2', 'category' => 'sociology','category_id' => 3],
        ['_id' => 'xxx','name' => 'economics book 1', 'category' => 'economics','category_id' => 4],
    ]);

I have no idea how to do it with laravel-mongodb,any suggestions?

Try like this:

$last_new_id = 1;

foreach($categories as $category){
    $category->id = $last_new_id ;
    $last_new_id++;
}

foreach($books as $book){
    $book->category_id = $categories->where('category',$book->category)->first()->id
}