Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

MongoDB insert order

by PhilipJ (Initiate)
on May 30, 2012 at 14:22 UTC ( #973294=perlquestion: print w/ replies, xml ) Need Help??
PhilipJ has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I am fooling with MongoDB and Perl and have a simple question - how do I maintain the order of data fields upon an insert? My code is the following: ############################################
#!usr/bin/perl 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" } );
#####################################

But when I lookup the record in mongoDB via the mongo query shell:
"_id" : ObjectId("4fc62c2900ece6040c000000"), "solution1" : "Hit the computer.", "product" : "WooHoo", "errcode" : "WM2001_89873", "line_text" : "Inserted in Perl too"
That's not the order I inserted! How do I fix this?

Comment on MongoDB insert order
Select or Download Code
Re: MongoDB insert order
by derby (Abbot) on May 30, 2012 at 14:59 UTC

    Laying aside that this may be a mongoDB internals issue, and laying aside possible Denial of Service attacks with deterministic keys, I'd have to ask "why does it even matter?"

    -derby
      I have javascript manipulating the results of a mongoDB find() and, while I could, of course, edit this so it were not the case, the script currently depends on the String input from the find() being in a specific order. That and it just seems like this should be an easy thing to do.

        Hmmm ... well ... it's not as easy as you think. It would mean that perl and mongo would share the same or similar associative array implementations. Even if they did, then your approach would never work 100% since perl does not even promise the key order will be the same in different processes.

        Did you mean "String *output* from find"? If so, you're probably better of converting the JSON returned from the find into a true JS object and manipulating it that way instead of doing any string manipulation on the results.

        -derby
Re: MongoDB insert order
by choroba (Abbot) on May 30, 2012 at 15:01 UTC
    Crossposted at StackOverflow. It is considered polite to inform about crossposting so people not visiting both sites do not waste their time on a problem already solved at the other site.
      Right on - I'll keep that in mind in future cross-postings.
Re: MongoDB insert order
by BrowserUk (Pope) on May 30, 2012 at 15:10 UTC

    If insertion order is important to your application -- and nothing in your post suggests it ought to be -- maybe you should consider using a 'Capped collection':

    Capped collections MongoDB supports fixed-size collections called capped collections. This type of collection maintains insertion order and, once the specified size has been reached, behaves like a circular queue.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://973294]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-07-29 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls