Skip to content

Commit 4943793

Browse files
author
高鸣飞
committed
[bugfix] turn off mongodb auto cast ObjectID
1 parent adee4ce commit 4943793

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

lib/define/mongodb.js

+29-17
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ insert(table, sets, callback) {
5252
if (this._readonly) {
5353
return errNotAllowd(callback)
5454
}
55-
this._conn.collection(table).insert(_.cloneDeep(sets), (err, result) => {
56-
if (err) {
57-
return callback(err)
58-
}
59-
if (result.constructor === Object) {
60-
result = [result]
61-
}
62-
return callback(err, {
63-
affected_rows: result.length,
64-
docs: result,
65-
}, null)
55+
this._conn.collection(table).insert(
56+
_.cloneDeep(sets),
57+
{castIds: false},
58+
(err, result) => {
59+
if (err) {
60+
return callback(err)
61+
}
62+
if (result.constructor === Object) {
63+
result = [result]
64+
}
65+
return callback(err, {
66+
affected_rows: result.length,
67+
docs: result,
68+
}, null)
6669
})
6770
}
6871

@@ -85,7 +88,7 @@ update(table, sets, where, callback) {
8588
update_doc['$set'] = sets
8689
}
8790
this._conn.collection(table)
88-
.update(where, update_doc, {multi: true}, (err, result) => {
91+
.update(where, update_doc, {multi: true, castIds: false}, (err, result) => {
8992
if (err) {
9093
return callback(err)
9194
}
@@ -112,7 +115,7 @@ delete(table, where, callback) {
112115
if (this._readonly) {
113116
return errNotAllowd(callback)
114117
}
115-
this._conn.collection(table).remove(where, (err, result) => {
118+
this._conn.collection(table).remove(where, {castIds: false}, (err, result) => {
116119
if (err) {
117120
return callback(err)
118121
}
@@ -137,6 +140,7 @@ select(table, fields, where, callback) {
137140
return callback(err)
138141
}
139142
let _fields = fields === '*' ? {} : fields
143+
_fields.castIds = false
140144
this._conn.collection(table).find(where, _fields, (err, docs) => {
141145
return callback(err, docs, null)
142146
})
@@ -157,7 +161,9 @@ find(table, options, where, callback) {
157161
} catch (err) {
158162
return callback(err)
159163
}
160-
this._conn.collection(table).find(where, options, (err, docs) => {
164+
let _options = _.cloneDeep(options)
165+
_options.castIds = false
166+
this._conn.collection(table).find(where, _options, (err, docs) => {
161167
return callback(err, docs, null)
162168
})
163169
}
@@ -177,7 +183,9 @@ findOneAndUpdate(table, options, sets, where, callback) {
177183
} catch (err) {
178184
return callback(err)
179185
}
180-
this._conn.collection(table).findOneAndUpdate(where, sets,
186+
let _options = _.cloneDeep(options)
187+
_options.castIds = false
188+
this._conn.collection(table).findOneAndUpdate(where, sets, _options,
181189
(err, updated_doc) => {
182190
return callback(err, updated_doc, null)
183191
})
@@ -204,7 +212,9 @@ aggregate(table, options, pipeline, callback) {
204212
} catch (err) {
205213
return callback(err)
206214
}
207-
this._conn.collection(table).aggregate(pipeline, options, (err, docs) => {
215+
let _options = _.cloneDeep(options)
216+
_options.castIds = false
217+
this._conn.collection(table).aggregate(pipeline, _options, (err, docs) => {
208218
return callback(err, docs, null)
209219
})
210220
}
@@ -223,7 +233,9 @@ count(table, options, where, callback) {
223233
} catch (err) {
224234
return callback(err)
225235
}
226-
this._conn.collection(table).count(where, options, (err, count) => {
236+
let _options = _.cloneDeep(options)
237+
_options.castIds = false
238+
this._conn.collection(table).count(where, _options, (err, count) => {
227239
return callback(err, count, null)
228240
})
229241
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlx",
3-
"version": "4.3.1",
3+
"version": "4.3.2",
44
"description": "Database driver with extended features like mysql changelog/oplog, connection auto release.",
55
"main": "index.js",
66
"directories": {

0 commit comments

Comments
 (0)