forked from JacksonTian/doxmate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.txt
More file actions
365 lines (166 loc) · 5.45 KB
/
doc.txt
File metadata and controls
365 lines (166 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
什么是Mongoose?
Mongoose是基于node-mongodb-native开发的MongoDB nodejs驱动,可以在异步的环境下执行。
如何安装Mongoose?
npm install mongoose
如何使用Mongoose?
test.js代码:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
mongoose.connect('mongodb://localhost/test');
var TestSchema = new Schema({
user_id : {type : Number, index : true}
,username : {type : String}
});
var model_name = coll_name = 'taobao';
mongoose.model(model_name, TestSchema, coll_name);
var TAOBAO = mongoose.model(model_name, coll_name);
var taobao = new TAOBAO();
taobao.user_id = 1;
taobao.username = 'xuanhou';
taobao.save(function(err) {
if (err) {
console.log('save failed');
}
console.log('save success');
});
执行test.js
node test.js
查看是否执行成功:
xuanhou.ysh[@XXXX test]$ mongo
MongoDB shell version: 1.8.0
connecting to: test
> show collections
system.indexes
taobao
> db.taobao.find()
{ "_id" : ObjectId("4db65fafcb4312401d000001"), "user_id" : 1, "username" : "xuanhou" }
> exit
bye
为什么还要基于node-mongodb-native开发mongoose呢?
下面是node-mongodb-native实现的代码相信能让你了解为什么很多人选择Mongoose(callback层数太多):
var client = new Db('integration_tests_20', new Server("127.0.0.1", 27017, {}));
client.open(function(err, p_client) {
client.createCollection('test_insert', function(err, collection) {
client.collection('test_insert', function(err, collection) {
for(var i = 1; i < 1000; i++) {
collection.insert({c:1}, function(err, docs) {});
}
collection.insert({a:2}, function(err, docs) {
collection.insert({a:3}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1001, count);
// Locate all the entries using find
collection.find(function(err, cursor) {
cursor.toArray(function(err, results) {
test.assertEquals(1001, results.length);
test.assertTrue(results[0] != null);
// Let's close the db
client.close();
});
});
});
});
});
});
});
});
目前Mongoose的开发情况如何?
Mongoose已经实现了大部分功能,但还有些Mongodb提供的feature没有实现,所以也给使用者提供了打补丁的机会,下面是我最近给Mongoose打的两个补丁:
让mongoose支持distinct操作
Index: model.js
===================================================================
--- model.js (revision 12218)
+++ model.js (working copy)
@@ -492,6 +492,34 @@
};
/
+ distinct values for key
+
+ @param {String} key
+ @param {Object} conditions
+ @param {Function} optional callback
+ @api public
+ */
+
+Model.distinct = function (key, conditions, callback) {
+ if ('function' == typeof conditions) {
+ callback = conditions;
+ conditions = {};
+ }
+ var query = new Query(conditions, key).bind(this, 'distinct');
+ if ('undefined' == typeof callback)
+ return query;
+ var cQuery;
+ if (cQuery = this._cumulativeQuery) {
+ merge(query._conditions, cQuery._conditions);
+ if (query.options && cQuery.options)
+ merge(query.options, cQuery.options);
+ delete this._cumulativeQuery;
+ }
+ if (!query.model) query.bind(this, 'distinct');
+ return query.distinct(callback);
+};
+
+/
where enables a very nice sugary api for doing your queries.
For example, instead of writing:
User.find({age: {$gte: 21, $lte: 65}}, callback);
Index: query.js
===================================================================
--- query.js (revision 12218)
+++ query.js (working copy)
@@ -11,6 +11,10 @@
function Query (criteria, options) {
options = this.options = options || {};
+ if ('string' == typeof options) {
+ this._key = options;
+ options = this.options = {};
+ }
this.safe = options.safe
this._conditions = {};
if (criteria) this.find(criteria);
@@ -730,6 +734,24 @@
};
/**
+ Casts this._conditions and sends a distinct
+ command to mongodb. Invokes a callback upon
+ receiving results.
+
+ @param {Function} callback fn(err, cardinality)
+ @api public
+ /
+Query.prototype.distinct = function (callback) {
+ this.op = 'distinct';
+ var model = this.model;
+ this.cast(model);
+ var castQuery = this._conditions;
+ var key = this._key;
+ model.collection.distinct(key, castQuery, callback);
+ return this;
+};
+
+/*
Casts the query, sends the update command to mongodb.
If there is an error, the callback handles it. Otherwise,
we just invoke the callback whereout passing it an error.
让$gt/$gte/$lt/$lte等操作支持字符串参数
Index: string.js
===================================================================
--- string.js (revision 12218)
+++ string.js (working copy)
@@ -139,10 +139,15 @@
}
SchemaString.prototype.$conditionalHandlers = {
- '$ne': handleSingle
+ '$lt': handleSingle
+ , '$lte': handleSingle
+ , '$gt': handleSingle
+ , '$gte': handleSingle
+ , '$ne': handleSingle
, '$in': handleArray
, '$nin': handleArray
};
+
SchemaString.prototype.castForQuery = function ($conditional, val) {
var handler;
if (arguments.length === 2) {