This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $client = MongoDB->connect(); | |
my $coll = $client->ns("test.foo"); | |
# do insert with number-like strings inserted as numbers | |
{ | |
my $coll2 = $coll->with_codec( prefer_numeric => 1 ); | |
$coll2->insert( answer => "42" ); # inserted as numeric 42 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $client = MongoDB->connect(); | |
my $coll = $client->ns("test.foo"); | |
# do insert with w:majority | |
{ | |
my $coll2 = $coll->clone( write_concern => { w => 'majority' } ); | |
$coll2->insert( $doc ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WRONG WAY IS FATAL | |
$client = MongoDB->connect() | |
$client->authenticate("foo", "username", "sekret"); | |
# RIGHT WAY | |
$client = MongoDB->connect("mongodb://username:sekret@localhost/foo"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $client = MongoDB->connect(); | |
my $coll = $client->ns("test.foo"); | |
# assume first operation fails due to network error | |
eval { | |
$coll->insert( $doc ); | |
}; | |
warn $@ if $@; | |
# next operation automatically reconnects to MongoDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RIGHT WAY | |
my $client = MongoDB->connect(); | |
eval { | |
# do work | |
}; | |
if ( $@ ) { | |
die "Error doing work: $@"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WRONG WAY | |
my $client; | |
if ( eval { $client = MongoDB->connect() } ) { | |
# do work | |
} | |
else { | |
die "MongoDB not available!"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# REPLICA SET (discovers other nodes) | |
my $client = MongoDB->connect( | |
"mongodb://h1.example.com/?replicaSet=myset" | |
); | |
# DIRECT CONNECTION | |
my $direct = MongoDB->connect( | |
"mongodb://h1.example.com/" | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use MongoDB; | |
# OLD WAY | |
my $client = MongoDB::MongoClient->new( | |
host => "mongodb://h1.example.com/" | |
); | |
# NEW WAY | |
my $client = MongoDB->connect("mongodb://h1.example.com/"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.collection(‘docs’).find({a:1}).limit(1).skip(100).toArray(function(err, docs) { | |
console.dir(err); | |
console.dir(docs); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ itemId: 2, recom: [ { itemId: 32, weight: 36}, | |
{ itemId: 158, weight: 23}, | |
… ] } |
NewerOlder