mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-29 08:43:57 +00:00
#166 filter by mongo ObjectID
This commit is contained in:
@@ -28,6 +28,20 @@ const numberTestCondition = () => value => ({
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const objectIdTestCondition = () => value => ({
|
||||||
|
$or: [
|
||||||
|
{
|
||||||
|
__placeholder__: {
|
||||||
|
$regex: `.*${value}.*`,
|
||||||
|
$options: 'i',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
__placeholder__: { $oid: value },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
const testCondition = (operator, value) => () => ({
|
const testCondition = (operator, value) => () => ({
|
||||||
__placeholder__: {
|
__placeholder__: {
|
||||||
[operator]: value,
|
[operator]: value,
|
||||||
@@ -64,9 +78,12 @@ const createParser = () => {
|
|||||||
.map(Number)
|
.map(Number)
|
||||||
.desc('number'),
|
.desc('number'),
|
||||||
|
|
||||||
|
objectid: () => token(P.regexp(/[0-9a-f]{24}/)).desc('ObjectId'),
|
||||||
|
|
||||||
noQuotedString: () => P.regexp(/[^\s^,^'^"]+/).desc('string unquoted'),
|
noQuotedString: () => P.regexp(/[^\s^,^'^"]+/).desc('string unquoted'),
|
||||||
|
|
||||||
value: r => P.alt(r.string1, r.string2, r.number, r.noQuotedString),
|
value: r => P.alt(r.objectid, r.string1, r.string2, r.number, r.noQuotedString),
|
||||||
|
valueTestObjectId: r => r.objectid.map(objectIdTestCondition()),
|
||||||
valueTestNum: r => r.number.map(numberTestCondition()),
|
valueTestNum: r => r.number.map(numberTestCondition()),
|
||||||
valueTest: r => r.value.map(regexCondition('.*#VALUE#.*')),
|
valueTest: r => r.value.map(regexCondition('.*#VALUE#.*')),
|
||||||
|
|
||||||
@@ -108,6 +125,7 @@ const createParser = () => {
|
|||||||
r.startsWithNot,
|
r.startsWithNot,
|
||||||
r.endsWithNot,
|
r.endsWithNot,
|
||||||
r.containsNot,
|
r.containsNot,
|
||||||
|
r.valueTestObjectId,
|
||||||
r.valueTestNum,
|
r.valueTestNum,
|
||||||
r.valueTest
|
r.valueTest
|
||||||
).trim(whitespace),
|
).trim(whitespace),
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
if (!filters[uniqueName]) continue;
|
if (!filters[uniqueName]) continue;
|
||||||
try {
|
try {
|
||||||
const ast = parseFilter(filters[uniqueName], 'mongo');
|
const ast = parseFilter(filters[uniqueName], 'mongo');
|
||||||
|
// console.log('AST', ast);
|
||||||
const cond = _.cloneDeepWith(ast, expr => {
|
const cond = _.cloneDeepWith(ast, expr => {
|
||||||
if (expr.__placeholder__) {
|
if (expr.__placeholder__) {
|
||||||
return {
|
return {
|
||||||
@@ -112,7 +113,6 @@
|
|||||||
|
|
||||||
return response.data.count;
|
return response.data.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
@@ -229,7 +229,6 @@
|
|||||||
|
|
||||||
{ command: 'collectionDataGrid.export', tag: 'export' }
|
{ command: 'collectionDataGrid.export', tag: 'export' }
|
||||||
);
|
);
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<LoadingDataGridCore
|
<LoadingDataGridCore
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ function readCursor(cursor, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const mongoIdRegex = /^[0-9a-f]{24}$/;
|
const mongoIdRegex = /^[0-9a-f]{24}$/;
|
||||||
function convertCondition(condition) {
|
function convertConditionInternal(condition) {
|
||||||
if (condition && _.isString(condition._id) && condition._id.match(mongoIdRegex)) {
|
if (condition && _.isString(condition._id) && condition._id.match(mongoIdRegex)) {
|
||||||
return {
|
return {
|
||||||
_id: ObjectId(condition._id),
|
_id: ObjectId(condition._id),
|
||||||
@@ -27,6 +27,12 @@ function convertCondition(condition) {
|
|||||||
return condition;
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function convertConditionUser(condition) {
|
||||||
|
return _.cloneDeepWith(condition, (x) => {
|
||||||
|
if (x && x.$oid) return ObjectId(x.$oid);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function findArrayResult(resValue) {
|
function findArrayResult(resValue) {
|
||||||
if (!_.isPlainObject(resValue)) return null;
|
if (!_.isPlainObject(resValue)) return null;
|
||||||
const arrays = _.values(resValue).filter((x) => _.isArray(x));
|
const arrays = _.values(resValue).filter((x) => _.isArray(x));
|
||||||
@@ -191,10 +197,11 @@ const driver = {
|
|||||||
try {
|
try {
|
||||||
const collection = pool.__getDatabase().collection(options.pureName);
|
const collection = pool.__getDatabase().collection(options.pureName);
|
||||||
if (options.countDocuments) {
|
if (options.countDocuments) {
|
||||||
const count = await collection.countDocuments(options.condition || {});
|
const count = await collection.countDocuments(convertConditionUser(options.condition) || {});
|
||||||
return { count };
|
return { count };
|
||||||
} else {
|
} else {
|
||||||
let cursor = await collection.find(options.condition || {});
|
// console.log('options.condition', JSON.stringify(options.condition, undefined, 2));
|
||||||
|
let cursor = await collection.find(convertConditionUser(options.condition) || {});
|
||||||
if (options.sort) cursor = cursor.sort(options.sort);
|
if (options.sort) cursor = cursor.sort(options.sort);
|
||||||
if (options.skip) cursor = cursor.skip(options.skip);
|
if (options.skip) cursor = cursor.skip(options.skip);
|
||||||
if (options.limit) cursor = cursor.limit(options.limit);
|
if (options.limit) cursor = cursor.limit(options.limit);
|
||||||
@@ -230,22 +237,24 @@ const driver = {
|
|||||||
...update.document,
|
...update.document,
|
||||||
...update.fields,
|
...update.fields,
|
||||||
};
|
};
|
||||||
const doc = await collection.findOne(convertCondition(update.condition));
|
const doc = await collection.findOne(convertConditionInternal(update.condition));
|
||||||
if (doc) {
|
if (doc) {
|
||||||
const resdoc = await collection.replaceOne(convertCondition(update.condition), {
|
const resdoc = await collection.replaceOne(convertConditionInternal(update.condition), {
|
||||||
...document,
|
...document,
|
||||||
_id: doc._id,
|
_id: doc._id,
|
||||||
});
|
});
|
||||||
res.replaced.push(resdoc._id);
|
res.replaced.push(resdoc._id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const resdoc = await collection.updateOne(convertCondition(update.condition), { $set: update.fields });
|
const resdoc = await collection.updateOne(convertConditionInternal(update.condition), {
|
||||||
|
$set: update.fields,
|
||||||
|
});
|
||||||
res.updated.push(resdoc._id);
|
res.updated.push(resdoc._id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const del of changeSet.deletes) {
|
for (const del of changeSet.deletes) {
|
||||||
const collection = db.collection(del.pureName);
|
const collection = db.collection(del.pureName);
|
||||||
const resdoc = await collection.deleteOne(convertCondition(del.condition));
|
const resdoc = await collection.deleteOne(convertConditionInternal(del.condition));
|
||||||
res.deleted.push(resdoc._id);
|
res.deleted.push(resdoc._id);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user