diff --git a/docs/reference/crud-operations.txt b/docs/reference/crud-operations.txt index b8e5d5634b..bbae4c202a 100644 --- a/docs/reference/crud-operations.txt +++ b/docs/reference/crud-operations.txt @@ -458,7 +458,7 @@ part of the command: .. code-block:: ruby - client.database.command(collstats: 'test', readConcern: {level: :majority}) + client.database.command(dbStats: 1, readConcern: {level: :majority}) .. _read-preference: @@ -504,7 +504,7 @@ option when a command is run on a database: } ) # Set read preference for a given command - client.database.command( { collstats: 'test' }, read: { mode: secondary, + client.database.command( { dbStats: 1 }, read: { mode: secondary, tag_sets: [ { 'dc' => 'nyc' } ] } ) Read preference can also be set for specific operations on a collection diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index bb828ce491..5b8d5af128 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -314,7 +314,9 @@ def with(new_options) # # @since 2.0.0 def capped? - database.read_command(:collstats => name).documents[0][CAPPED] + database.list_collections(filter: { name: name }) + .first + &.dig('options', CAPPED) || false end # Force the collection to be created in the database. diff --git a/lib/mongo/operation/shared/sessions_supported.rb b/lib/mongo/operation/shared/sessions_supported.rb index e8d59c23d4..aed9bd23e6 100644 --- a/lib/mongo/operation/shared/sessions_supported.rb +++ b/lib/mongo/operation/shared/sessions_supported.rb @@ -30,7 +30,6 @@ module SessionsSupported READ_COMMANDS = [ :aggregate, - :collStats, :count, :dbStats, :distinct, diff --git a/spec/integration/docs_examples_spec.rb b/spec/integration/docs_examples_spec.rb index 01609bc353..bf1ae9e1b0 100644 --- a/spec/integration/docs_examples_spec.rb +++ b/spec/integration/docs_examples_spec.rb @@ -162,7 +162,7 @@ # Start runCommand Example 2 - client.database.command(collStats: 'restaurants') + client.database.command(dbStats: 1) # End runCommand Example 2 end diff --git a/spec/mongo/collection_ddl_spec.rb b/spec/mongo/collection_ddl_spec.rb index 4599133925..1c644cfabd 100644 --- a/spec/mongo/collection_ddl_spec.rb +++ b/spec/mongo/collection_ddl_spec.rb @@ -137,7 +137,11 @@ end let(:collstats) do - database.read_command(:collstats => :specs).documents.first + collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first + end + + let(:storage_stats) do + collstats.fetch('storageStats', {}) end let(:options) do @@ -157,9 +161,9 @@ end it "applies the options" do - expect(collstats["capped"]).to be true - expect(collstats["max"]).to eq(512) - expect(collstats["maxSize"]).to eq(4096) + expect(storage_stats["capped"]).to be true + expect(storage_stats["max"]).to eq(512) + expect(storage_stats["maxSize"]).to eq(4096) end end end diff --git a/spec/mongo/collection_spec.rb b/spec/mongo/collection_spec.rb index bd050bb339..ef58e44c84 100644 --- a/spec/mongo/collection_spec.rb +++ b/spec/mongo/collection_spec.rb @@ -651,7 +651,11 @@ end let(:collstats) do - database.read_command(:collstats => :specs).documents.first + collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first + end + + let(:storage_stats) do + collstats.fetch('storageStats', {}) end before do @@ -664,9 +668,9 @@ end it "applies the options" do - expect(collstats["capped"]).to be true - expect(collstats["max"]).to eq(512) - expect(collstats["maxSize"]).to eq(4096) + expect(storage_stats["capped"]).to be true + expect(storage_stats["max"]).to eq(512) + expect(storage_stats["maxSize"]).to eq(4096) end end diff --git a/spec/runners/unified/test.rb b/spec/runners/unified/test.rb index d1f21c5f60..bc48a01d65 100644 --- a/spec/runners/unified/test.rb +++ b/spec/runners/unified/test.rb @@ -320,22 +320,22 @@ def set_initial_data collection = root_authorized_client.with(write_concern: {w: :majority}). use(spec.use!('databaseName'))[spec.use!('collectionName')] collection.drop + create_options = spec.use('createOptions') || {} docs = spec.use!('documents') - if docs.any? - collection.insert_many(docs) - else - begin - collection.create - rescue Mongo::Error => e - if Mongo::Error::OperationFailure === e && ( + begin + collection.create(create_options) + rescue Mongo::Error => e + if Mongo::Error::OperationFailure === e && ( e.code == 48 || e.message =~ /collection already exists/ - ) - # Already exists - else - raise - end + ) + # Already exists + else + raise end end + if docs.any? + collection.insert_many(docs) + end unless spec.empty? raise NotImplementedError, "Unhandled spec keys: #{spec}" end diff --git a/spec/spec_tests/data/client_side_encryption/bypassedCommand.yml b/spec/spec_tests/data/client_side_encryption/bypassedCommand.yml index 7057abd592..98e360b6e3 100644 --- a/spec/spec_tests/data/client_side_encryption/bypassedCommand.yml +++ b/spec/spec_tests/data/client_side_encryption/bypassedCommand.yml @@ -26,7 +26,7 @@ tests: command: ping: 1 command_name: ping - - description: "current op is not bypassed" + - description: "kill op is not bypassed" clientOptions: autoEncryptOpts: kmsProviders: @@ -34,9 +34,10 @@ tests: operations: - name: runCommand object: database - command_name: currentOp + command_name: killOp arguments: command: - currentOp: 1 + killOp: 1 + op: 1234 result: - errorContains: "command not supported for auto encryption: currentOp" \ No newline at end of file + errorContains: "command not supported for auto encryption: killOp" \ No newline at end of file diff --git a/spec/spec_tests/data/unified/valid-pass/collectionData-createOptions.yml b/spec/spec_tests/data/unified/valid-pass/collectionData-createOptions.yml new file mode 100644 index 0000000000..c6afedcfa9 --- /dev/null +++ b/spec/spec_tests/data/unified/valid-pass/collectionData-createOptions.yml @@ -0,0 +1,37 @@ +description: collectionData-createOptions +schemaVersion: "1.9" +runOnRequirements: + - minServerVersion: "3.6" + # Capped collections cannot be created on serverless instances. + serverless: forbid +createEntities: + - client: + id: &client0 client0 + - database: + id: &database0 database0 + client: *client0 + databaseName: &database0Name database0 + - collection: + id: &collection0 collection0 + database: *database0 + collectionName: &collection0Name coll0 +initialData: + - collectionName: *collection0Name + databaseName: *database0Name + createOptions: + capped: true + # With MMAPv1, the size field cannot be less than 4096. + size: &cappedSize 4096 + documents: + - { _id: 1, x: 11 } +tests: + - description: collection is created with the correct options + operations: + - object: *collection0 + name: aggregate + arguments: + pipeline: + - $collStats: { storageStats: {} } + - $project: { capped: '$storageStats.capped', maxSize: '$storageStats.maxSize'} + expectResult: + - { capped: true, maxSize: *cappedSize } \ No newline at end of file