I have been fooling with the Perl MongoDB library and have been hard pressed figuring out how to do something pretty simple.
How do I maintain the order of data fields upon an insert? My code is the following:
use MongoDB;
use MongoDB::Database;
use MongoDB::OID;
my $conn = MongoDB::Connection->new;
my $db = $conn->test;
my $users = $db->testlogwiki;
$users->insert
(
{
"product" => "WooHoo",
"errcode" => "WM2001_89873",
"solution1" => "Hit the computer.",
"line_text" => "Inserted in Perl too"
}
);
When I go back and look up in my MongoDB how the record has been inserted it looks like this:
db.testlogwiki.find([criteria that finds it]).pretty();
"_id" : ObjectId("4fc62c2900ece6040c000000"),
"solution1" : "Hit the computer.",
"product" : "WooHoo",
"errcode" : "WM2001_89873",
"line_text" : "Inserted in Perl too"
That’s not the order I want…how do I make it the order I want?
Both Perl and Mongo’s BSON hashes are unordered by definition. If you need to order properties in some way, you must keep track of it yourself.