Skip to content

Instantly share code, notes, and snippets.

TEST_USERS = [{'fn' : 'Test', 'ln' : 'User1',
'email' : 'testuser1@example.com', 'pwd' : 'testUser123'},
{'fn' : 'Test', 'ln' : 'User2',
'email' : 'testuser2@example.com', 'pwd' : 'testUser123'},
{'fn' : 'Test', 'ln' : 'User3',
'email' : 'testuser3@example.com', 'pwd' : 'testUser123'}]
SP_APP_NAME = 'Reader Test';
var frisby = require('frisby');
function dropUserCollection(callback) {
console.log("dropUserCollection");
user = reader_test_db.collection('user');
if (undefined != user) {
user.drop(function(err, reply) {
console.log('user collection dropped');
callback(0);
});
} else {
callback(0);
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
}
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 );
}
# WRONG WAY IS FATAL
$client = MongoDB->connect()
$client->authenticate("foo", "username", "sekret");
# RIGHT WAY
$client = MongoDB->connect("mongodb://username:sekret@localhost/foo");
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
# RIGHT WAY
my $client = MongoDB->connect();
eval {
# do work
};
if ( $@ ) {
die "Error doing work: $@";
}
# WRONG WAY
my $client;
if ( eval { $client = MongoDB->connect() } ) {
# do work
}
else {
die "MongoDB not available!";
}
# 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/"
);
use MongoDB;
# OLD WAY
my $client = MongoDB::MongoClient->new(
host => "mongodb://h1.example.com/"
);
# NEW WAY
my $client = MongoDB->connect("mongodb://h1.example.com/");