Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (67 sloc) 4.067 kB
3d268a1 first docs
andris9 authored
1 Collections
2 ===========
3
4f1d12c docs update
andris9 authored
4 See also:
5
6 * [Database](database.md)
7 * [Queries](queries.md)
8
9 ## Collection objects
10
f9af2a8 Update docs/collections.md
Mahmoud Abounassif authored
11 Collection object is a pointer to a specific collection in the [database](database.md). If you want to [insert](insert.md) new records or
f9766c8 docs update
andris9 authored
12 [query](queries.md) existing ones then you need to have a valid collection object.
3d268a1 first docs
andris9 authored
13
047c7a4 updated readme
andris9 authored
14 **NB** Collection names can't start or end with a period nor contain a dollar sign! (`.tes$t` is not allowed)
15
3d268a1 first docs
andris9 authored
16 ## Creating collections
17
18 Collections can be created with `createCollection`
19
4571af8 @christkv Added some more tests, small fixes, cleanup and docs
authored
20 db.createCollection([[name[, options]], callback)
21
22 where `name` is the name of the collection, options a set of configuration parameters and `callback` is a callback function. `db` is the database object.
3d268a1 first docs
andris9 authored
23
a0105bb @christkv Attempting reformating
authored
24 The first parameter for the callback is the error object (null if no error) and the second one is the pointer to the newly created collection. If strict mode is on and the table exists, the operation yields in error. With strict mode off (default) the function simple returns the pointer to the existing collection and does not truncate it.
3d268a1 first docs
andris9 authored
25
26 db.createCollection("test", function(err, collection){
27 collection.insert({"test":"value"});
28 });
29
4571af8 @christkv Added some more tests, small fixes, cleanup and docs
authored
30 ## Creating collections options
31 Several options can be passed to the `createCollection` function with `options` parameter.
32
33 * `raw` - driver returns documents as bson binary Buffer objects, `default:false`
34
c44fc5b listed collection and db properties
andris9 authored
35 ### Collection properties
36
37 * `collectionName` is the name of the collection (not including the database name as a prefix)
38 * `db` is the pointer to the corresponding databse object
fc12118 listed collection and db properties
andris9 authored
39
40 Example of usage:
41
c44fc5b listed collection and db properties
andris9 authored
42 console.log("Collection name: "+collection.collectionName)
43
9dcb15e added gridstore
andris9 authored
44 ## List existing collections
45
c44fc5b listed collection and db properties
andris9 authored
46 ### List names
47
9dcb15e added gridstore
andris9 authored
48 Collections can be listed with `collectionNames`
49
50 db.collectionNames(callback);
51
52 `callback` gets two parameters - an error object (if error occured) and an array of collection names as strings.
53
54 Collection names also include database name, so a collection named `posts` in a database `blog` will be listed as `blog.posts`.
55
a0105bb @christkv Attempting reformating
authored
56 Additionally there's system collections which should not be altered without knowing exactly what you are doing, these sollections can be identified with `system` prefix. For example `posts.system.indexes`.
9dcb15e added gridstore
andris9 authored
57
58 Example:
59
60 var mongodb = require("mongodb"),
61 mongoserver = new mongodb.Server("localhost"),
62 db_connector = new mongodb.Db("blog", mongoserver);
63
64 db_connector.open(function(err, db){
65 db.collectionNames(function(err, collections){
66 console.log(collections); // ["blog.posts", "blog.system.indexes"]
67 });
68 });
c44fc5b listed collection and db properties
andris9 authored
69
70 ## List collections
71
72 Collection objects can be listed with database method `collections`
73
74 db.collections(callback)
75
76 Where `callback` gets two parameters - an error object (if an error occured) and an array of collection objects.
9dcb15e added gridstore
andris9 authored
77
3d268a1 first docs
andris9 authored
78 ## Selecting collections
79
80 Existing collections can be opened with `collection`
81
4571af8 @christkv Added some more tests, small fixes, cleanup and docs
authored
82 db.collection([[name[, options]], callback);
3d268a1 first docs
andris9 authored
83
84 If strict mode is off, then a new collection is created if not already present.
85
4571af8 @christkv Added some more tests, small fixes, cleanup and docs
authored
86 ## Selecting collections options
87 Several options can be passed to the `collection` function with `options` parameter.
88
89 * `raw` - driver returns documents as bson binary Buffer objects, `default:false`
90
047c7a4 updated readme
andris9 authored
91 ## Renaming collections
92
93 A collection can be renamed with collection method `rename`
94
95 collection.rename(new_name, callback);
96
7af31e9 updated readme
andris9 authored
97 ## Removing records from collections
3d268a1 first docs
andris9 authored
98
8ce3cb4 updated readme
andris9 authored
99 Records can be erased from a collection with `remove`
3d268a1 first docs
andris9 authored
100
7af31e9 updated readme
andris9 authored
101 collection.remove([[query[, options]], callback]);
102
103 Where
104
8ce3cb4 updated readme
andris9 authored
105 * `query` is the query that records to be removed need to match. If not set all records will be removed
7af31e9 updated readme
andris9 authored
106 * `options` indicate advanced options. For example use `{safe: true}` when using callbacks
1a2c8fa docs updates
andris9 authored
107 * `callback` callback function that gets two parameters - an error object (if an error occured) and the count of removed records
3d268a1 first docs
andris9 authored
108
109 ## Removing collections
110
111 A collection can be dropped with `drop`
112
9dcb15e added gridstore
andris9 authored
113 collection.drop(callback);
114
115 or with `dropCollection`
116
117 db.dropCollection(collection_name, callback)
Something went wrong with that request. Please try again.