File tree Expand file tree Collapse file tree 6 files changed +61
-1
lines changed Expand file tree Collapse file tree 6 files changed +61
-1
lines changed Original file line number Diff line number Diff line change @@ -523,6 +523,13 @@ describe('e2e', function() {
523
523
expect ( await shell . executeLine ( 'typeof JSON.parse(JSON.stringify(db.test.insertOne({}))).insertedId' ) ) . to . include ( 'string' ) ;
524
524
} ) ;
525
525
526
+ context ( 'post-4.2' , ( ) => {
527
+ skipIfServerVersion ( testServer , '< 4.4' ) ;
528
+ it ( 'allows calling convertShardKeyToHashed() as a global function' , async function ( ) {
529
+ expect ( await shell . executeLine ( 'convertShardKeyToHashed({foo:"bar"})' ) ) . to . include ( 'Long("4975617422686807705")' ) ;
530
+ } ) ;
531
+ } ) ;
532
+
526
533
describe ( 'document validation errors' , ( ) => {
527
534
context ( 'post-4.4' , ( ) => {
528
535
skipIfServerVersion ( testServer , '<= 4.4' ) ;
Original file line number Diff line number Diff line change 38
38
39
39
FAILED=no
40
40
41
- CONNECTION_STATUS_COMMAND=' db.runCommand({ connectionStatus: 1 }).authInfo.authenticatedUsers'
41
+ # convertShardKeyToHashed() may seem weird to include here, but it's the best
42
+ # way to make sure that it works in our standard environments we connect to
43
+ CONNECTION_STATUS_COMMAND=' convertShardKeyToHashed("asdf");db.runCommand({ connectionStatus: 1 }).authInfo.authenticatedUsers'
42
44
CONNECTION_STATUS_CHECK_STRING=" user: '${ATLAS_USERNAME} '"
43
45
44
46
function check_failed() {
Original file line number Diff line number Diff line change @@ -168,6 +168,10 @@ const translations: Catalog = {
168
168
} ,
169
169
isInteractive : {
170
170
description : 'Returns whether the shell will enter or has entered interactive mode'
171
+ } ,
172
+ convertShardKeyToHashed : {
173
+ description : 'Returns the hashed value for the input using the same hashing function as a hashed index.' ,
174
+ link : 'https://www.mongodb.com/docs/manual/reference/method/convertShardKeyToHashed/'
171
175
}
172
176
}
173
177
} ,
@@ -1798,6 +1802,10 @@ const translations: Catalog = {
1798
1802
getClientEncryption : {
1799
1803
description : 'Returns the ClientEncryption object for the current database collection. The ClientEncryption object supports explicit (manual) encryption and decryption of field values for Client-Side field level encryption.' ,
1800
1804
link : 'https://docs.mongodb.com/manual/reference/method/getClientEncryption/#getClientEncryption'
1805
+ } ,
1806
+ convertShardKeyToHashed : {
1807
+ description : 'Returns the hashed value for the input using the same hashing function as a hashed index.' ,
1808
+ link : 'https://www.mongodb.com/docs/manual/reference/method/convertShardKeyToHashed/'
1801
1809
}
1802
1810
}
1803
1811
} ,
Original file line number Diff line number Diff line change @@ -2047,6 +2047,15 @@ describe('Shell API (integration)', function() {
2047
2047
}
2048
2048
} ) ;
2049
2049
} ) ;
2050
+ describe ( 'convertShardKeyToHashed' , ( ) => {
2051
+ skipIfServerVersion ( testServer , '< 4.4' ) ;
2052
+
2053
+ it ( 'converts a shard key to its hashed representation' , async ( ) => {
2054
+ const result = await mongo . convertShardKeyToHashed ( { foo : 'bar' } ) ;
2055
+ expect ( result . constructor . name ) . to . equal ( 'Long' ) ;
2056
+ expect ( result . toString ( ) ) . to . equal ( '4975617422686807705' ) ;
2057
+ } ) ;
2058
+ } ) ;
2050
2059
} ) ;
2051
2060
describe ( 'PlanCache' , ( ) => {
2052
2061
skipIfApiStrict ( ) ;
Original file line number Diff line number Diff line change @@ -667,4 +667,33 @@ export default class Mongo extends ShellApiClass {
667
667
}
668
668
return this . _keyVault ;
669
669
}
670
+
671
+ @returnsPromise
672
+ async convertShardKeyToHashed ( value : any ) : Promise < unknown > {
673
+ const pipeline = [
674
+ { $limit : 1 } ,
675
+ { $project : { _id : { $toHashedIndexKey : { $literal : value } } } }
676
+ ] ;
677
+ let result ;
678
+ try {
679
+ // Try $documents if available.
680
+ result = await ( await this . getDB ( 'admin' ) . aggregate ( [ { $documents : [ { } ] } , ...pipeline ] ) ) . next ( ) ;
681
+ } catch {
682
+ try {
683
+ // If that fails, try a default collection like admin.system.version.
684
+ result = await ( await this . getDB ( 'admin' ) . getCollection ( 'system.version' ) . aggregate ( pipeline ) ) . next ( ) ;
685
+ } catch {
686
+ // If that fails, try using $collStats for local.oplog.rs.
687
+ try {
688
+ result = await ( await this . getDB ( 'local' ) . getCollection ( 'oplog.rs' ) . aggregate ( [ { $collStats : { } } , ...pipeline ] ) ) . next ( ) ;
689
+ } catch { /* throw exception below */ }
690
+ }
691
+ }
692
+ if ( ! result ) {
693
+ throw new MongoshRuntimeError (
694
+ 'Could not find a suitable way to run convertShardKeyToHashed() -- tried $documents and aggregating on admin.system.version and local.oplog.rs' ,
695
+ CommonErrors . CommandFailed ) ;
696
+ }
697
+ return result . _id ;
698
+ }
670
699
}
Original file line number Diff line number Diff line change @@ -316,6 +316,11 @@ export default class ShellApi extends ShellApiClass {
316
316
await this . _print ( origArgs , 'printjson' ) ;
317
317
}
318
318
319
+ @returnsPromise
320
+ async convertShardKeyToHashed ( value : any ) : Promise < unknown > {
321
+ return this . _instanceState . currentDb . _mongo . convertShardKeyToHashed ( value ) ;
322
+ }
323
+
319
324
@directShellCommand
320
325
@returnsPromise
321
326
async cls ( ) : Promise < void > {
You can’t perform that action at this time.
0 commit comments