Preheat the oven to 350 degrees F. Grease a 9-by-3-inch cake pan with cooking spray.
Bring a large pot of salted water to a boil. Add the spaghetti and cook for 3 to 4 minutes less than the package recommends, so that it is very al dente. Drain and reserve.
In a large skillet over medium-high heat, brown the beef, 5 to 8 minutes; season with salt and pepper. Transfer the beef to a paper towel-lined plate to drain. Wipe the skillet clean with a paper towel. Heat the olive oil in the skillet over medium heat and saute the onions until translucent, 4 to 5 minutes. Set aside.
In a large bowl, whisk together the marinara, ricotta, parsley, eggs, 1/2 cup of the Parmesan, 1 teaspoon salt and 1/4 teaspoon pepper. Add the cooked spaghetti, beef and onions and toss to evenly coat. Transfer to the prepared pan, top with the mozzarella and sprinkle with the remaining 2 tablespoons Parmesan.
Bake until the cheese is bubbling and golden brown, 25 to 30 minutes. Let rest for 5 minutes, then cut into wedges and serve.