1MongoDB::Database(3) User Contributed Perl Documentation MongoDB::Database(3)
2
3
4
6 MongoDB::Database - A Mongo database
7
9 The MongoDB::Database class accesses to a database.
10
11 # accesses the foo database
12 my $db = $connection->foo;
13
14 You can also access databases with the "get_database($name)" in
15 MongoDB::Connection method.
16
18 Core documentation on databases:
19 <http://dochub.mongodb.org/core/databases>.
20
22 name
23 The name of the database.
24
26 collection_names
27 my @collections = $database->collection_names;
28
29 Returns the list of collections in this database.
30
31 get_collection ($name)
32 my $collection = $database->get_collection('foo');
33
34 Returns a MongoDB::Collection for the collection called $name within
35 this database.
36
37 get_gridfs ($prefix?)
38 my $grid = $database->get_gridfs;
39
40 Returns a MongoDB::GridFS for storing and retrieving files from the
41 database. Default prefix is "fs", making "$grid->files" "fs.files" and
42 "$grid->chunks" "fs.chunks".
43
44 See MongoDB::GridFS for more information.
45
46 drop
47 $database->drop;
48
49 Deletes the database.
50
51 last_error($options?)
52 my $err = $db->last_error({w => 2});
53
54 Finds out if the last database operation completed successfully. If
55 the last operation did not complete successfully, returns a hash
56 reference of information about the error that occured.
57
58 The optional $options parameter is a hash reference that can contain
59 any of the following:
60
61 w Guarantees that the previous operation will be replicated to "w"
62 servers before this command will return success. See
63 "MongoDB::Connection::w" for more information.
64
65 wtimeout
66 Milliseconds to wait for "w" copies of the data to be made. This
67 parameter should generally be specified, as the database will
68 otherwise wait forever if "w" copies cannot be made.
69
70 fsync
71 If true, the database will fsync to disk before returning.
72
73 "last_error" returns a hash with fields that vary, depending on what
74 the previous operation was and if it succeeded or failed. If the last
75 operation (before the "last_error" call) failed, either:
76
77 "err" will be set or
78 "errmsg" will be set and "ok" will be 0.
79
80 If "err" is "null" and "ok" is 1, the previous operation succeeded.
81
82 The fields in the hash returned can include (but are not limited to):
83
84 "ok"
85 This should almost be 1 (unless "last_error" itself failed).
86
87 "err"
88 If this field is non-null, an error occurred on the previous
89 operation. If this field is set, it will be a string describing the
90 error that occurred.
91
92 "code"
93 If a database error occurred, the relevant error code will be
94 passed back to the client.
95
96 "errmsg"
97 This field is set if something goes wrong with a database command.
98 It is coupled with "ok" being 0. For example, if "w" is set and
99 times out, "errmsg" will be set to "timed out waiting for slaves"
100 and "ok" will be 0. If this field is set, it will be a string
101 describing the error that occurred.
102
103 "n" If the last operation was an insert, an update or a remove, the
104 number of objects affected will be returned.
105
106 "wtimeout"
107 If the previous option timed out waiting for replication.
108
109 "waited"
110 How long the operation waited before timing out.
111
112 "wtime"
113 If "w" was set and the operation succeeded, how long it took to
114 replicate to "w" servers.
115
116 "upserted"
117 If an upsert occured, this field will contain the new record's
118 "_id" field. For upserts, either this field or "updatedExisting"
119 will be present (unless an error occurred).
120
121 "updatedExisting"
122 If an upsert updated an existing element, this field will be
123 "true". For upserts, either this field or "upserted" will be
124 present (unless an error occurred).
125
126 See "w" in MongoDB::Connection for more information.
127
128 run_command ($command)
129 my $result = $database->run_command({ some_command => 1 });
130
131 Runs a database command. Throws an exception with an error message if
132 the command fails. Returns the result of the command on success. For a
133 list of possible database commands, run:
134
135 my $commands = $db->run_command({listCommands : 1});
136
137 There are a few examples of database commands in the "DATABASE
138 COMMANDS" in MongoDB::Examples section.
139
140 See also core documentation on database commands:
141 <http://dochub.mongodb.org/core/commands>.
142
143 eval ($code, $args?)
144 my $result = $database->eval('function(x) { return "hello, "+x; }', ["world"]);
145
146 Evaluate a JavaScript expression on the Mongo server. The $code
147 argument can be a string or an instance of MongoDB::Code. The $args
148 are an optional array of arguments to be passed to the $code function.
149
150 "eval" is useful if you need to touch a lot of data lightly; in such a
151 scenario the network transfer of the data could be a bottleneck. The
152 $code argument must be a JavaScript function. $args is an array of
153 parameters that will be passed to the function. For more examples of
154 using eval see
155 http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-Using{{db.eval%28%29}}
156 <http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-
157 sideCodeExecution-Using{{db.eval%28%29}}>.
158
160 Kristina Chodorow <kristina@mongodb.org>
161
162
163
164perl v5.12.3 2011-01-19 MongoDB::Database(3)