簡體   English   中英

MongoDB和Perl:維護插入順序

[英]MongoDB and Perl: maintaining insert order

我一直在愚弄Perl MongoDB庫,並且很難找出如何做一些非常簡單的事情。

如何在插入時保持數據字段的順序? 我的代碼如下:

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"
   }
);

當我回頭查看MongoDB中的記錄是如何插入的,它看起來像這樣:

db.testlogwiki.find([找到它的條件])。pretty();

"_id" : ObjectId("4fc62c2900ece6040c000000"),
"solution1" : "Hit the computer.",
"product" : "WooHoo",
"errcode" : "WM2001_89873",
"line_text" : "Inserted in Perl too"

那不是我想要的訂單...我如何使其成為我想要的訂單?

根據定義,Perl和Mongo的BSON哈希都是無序的。 如果需要以某種方式訂購屬性,則必須自己對其進行跟蹤。

MongoDB不一定與此有關-Perl哈希並不首先保留順序:

$ perl -MData::Dumper -E 'my $foo = { one => 1, two => 2, three => 3 }; print Dumper($foo);'
$VAR1 = {
          'three' => 3,
          'one' => 1,
          'two' => 2
        };

您可以使用Tie :: IxHash創建散列,這些散列將在內存中保留其順序,但是我不能說在插入MongoDB或從MongoDB中檢索時是否還會使它們保留其順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM