Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 130 lines (98 sloc) 3.234 kB
867c267 @tdegrunt First new README and simple example
tdegrunt authored
1 # ODM
2
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
3 ODM is a new, innovative and easy way to use MongoDB documents, as Models, in your code.
4 It uses the [JSON schema](http://tools.ietf.org/html/draft-zyp-json-schema-03) standard for validating the documents.
5
6 ## Quick Start
7
8 ### Connect
c4eb34e @tdegrunt Documentation
tdegrunt authored
9
10 odm.connect('mongodb://127.0.0.1:27017/simple');
11
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
12 ### Define a model
c4eb34e @tdegrunt Documentation
tdegrunt authored
13
14 var Person = odm.model("persons", {
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
15 "type" : "object",
16 "properties": {
17 "name": {"type": "string"},
18 }
19 });
20
21 ### Embedding other documents
22
23 We embed an address model in person:
24
25 // Address, to be embedded on Person
26 var Address = odm.model({
27 "id": "Simple#Address",
28 "type" : "object",
29 "properties": {
30 "lines": {
31 "type": "array",
32 "items": {"type": "string"}
33 },
34 "zip": {"type": "string"},
35 "city": {"type": "string"},
36 "country": {"type": "string"}
37 }
38 });
39
40 The changed person model:
41
42 var Person = odm.model("persons", {
43 "type" : "object",
44 "properties": {
45 "name": {"type": "string"},
46 "address": {"$ref": "Simple#Address"}
47 }
c4eb34e @tdegrunt Documentation
tdegrunt authored
48 });
49
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
50
867c267 @tdegrunt First new README and simple example
tdegrunt authored
51 ## Methods
52
832b46a @tdegrunt Updated documentatio
tdegrunt authored
53 ### Class methods
7927e70 @pmlopes created helper function that extends json.parse to understand objecti…
authored
54
832b46a @tdegrunt Updated documentatio
tdegrunt authored
55 #### ODM#parse(jsonString)
56 Parses a JSON string to a JSON document. It is aware of ISO Dates and ObjectIds and coverts them on the fly.
57
58 #### Model#findOne(query, fields, options, callback)
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
59 Finds one document or `fields`, satisfying `query`.
60
832b46a @tdegrunt Updated documentatio
tdegrunt authored
61 Person.findOne({'name': 'Barack Obama'}, function (error, document) {
62 if (error) {
63 console.log("error", error);
64 }
65 console.log(document);
66 });
67 #### Model#findById(id, fields, options, callback)
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
68 Finds one document by `id`, returining `fields`.
69
832b46a @tdegrunt Updated documentatio
tdegrunt authored
70 Person.findById("4ff3fcf14335e9d6ba000001", function (error, document) {
71 if (error) {
72 console.log("error", error);
73 }
74 console.log(document);
75 });
76 #### Model#find(query, fields, options, callback)
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
77 Finds all documents or `fields`, satisfying `query`.
78
832b46a @tdegrunt Updated documentatio
tdegrunt authored
79 Person.find({'name': 'Barack Obama'}, function (error, documents) {
80 if (error) {
81 console.log("error", error);
82 }
83 console.log(documents);
84 });
85
86 #### Model#findAll(fields, options, callback)
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
87 Finds all documents or `fields`.
88
832b46a @tdegrunt Updated documentatio
tdegrunt authored
89 Person.findAll(function (error, documents) {
90 if (error) {
91 console.log("error", error);
92 }
93 console.log(documents);
94 });
95
96 #### Model#remove(query, options, callback)
9810ce3 @tdegrunt Updated samples, more documentation
tdegrunt authored
97 Removes all documents satisfying `query`.
98
832b46a @tdegrunt Updated documentatio
tdegrunt authored
99 #### Model#update(query, document, options, callback)
100 Update all documents satisfying `query`, with document.
101
2152dd4 @pmlopes added documentation
authored
102 #### Model#loadDbRef(id/ids, options, callback)
103 Loads one Id or array of ids, it is similar to a simple find, however the number of results and order is the same as the array argument
104
832b46a @tdegrunt Updated documentatio
tdegrunt authored
105 #### Model#ensureIndex(fieldOrSpec, options, callback)
106 Adds an index and will also add a `findByXXX` method, where XXX is the name of the `fieldOrSpec`
107
108 ### Instance methods
109
110 #### Model.validate(verbose)
111 Returns true or false, or all errors in case `verbose` is true.
112
113 #### Model.save(options, callback)
114 Saves the instance model.
115
116 p.save(function (error, id) {
117 if (error) {
118 console.log("error", error);
119 }
120 console.log(id);
121 });
122
123 #### Model.update(query, document, options, callback)
124 Update the instance model.
125
126 #### Model.insert(options, callback)
127 Insert the instance model.
867c267 @tdegrunt First new README and simple example
tdegrunt authored
128
832b46a @tdegrunt Updated documentatio
tdegrunt authored
129 #### Model.remove(options, callback)
130 Remove the instance model.
Something went wrong with that request. Please try again.