mongo 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1b1c17b52cc160be41aa083e33789a71d99b6b8
4
- data.tar.gz: ba505d3b59d858a210a570670a98c6baded60489
3
+ metadata.gz: d95f4cbddad1184a95d53cb257befaceb5b9d1b0
4
+ data.tar.gz: 2639f1045f2be8059283b80bcd4e7a867b1ee35c
5
5
  SHA512:
6
- metadata.gz: 9e770b0aee96c3226681ae96fb0de826fbb4c10ddb8851bd4d9ce763b0cd6d8c1067ec50f42748d84ac31e1ca009e641d2f7f66e780166a8cd4adc33e9db6183
7
- data.tar.gz: 5a34d970f35bcd33d06dbfc61ae533cd05a5f585bb242d9e591805d3545902d2cfd74071587bbd47f997318ad9012012c3ad4d951ca0f79ce9c5488324d890cf
6
+ metadata.gz: 806e70e8f37683ba17c3416a9eb4c6d662305c4b8138f62b9af82c234b3cd98c10d1cd3119040b2b5751208e2058cd57532b4dfd63acee6628da692e8af55bad
7
+ data.tar.gz: e5a1c81c7930634054d5ace16c452ef4ba0c313a1fc779c2cc3deca75910b7014cae202b73cdd659d50f47d2e66f549fbd1e6017651815fec257b09db6ff4bd6
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -119,7 +119,7 @@ module Mongo
119
119
  documents.each(&block)
120
120
  end
121
121
 
122
- # Initialize a new result result.
122
+ # Initialize a new result.
123
123
  #
124
124
  # @example Instantiate the result.
125
125
  # Result.new(replies)
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require 'mongo/operation/write/idable'
15
16
  require 'mongo/operation/write/bulk'
16
17
  require 'mongo/operation/write/delete'
17
18
  require 'mongo/operation/write/insert'
@@ -47,6 +47,7 @@ module Mongo
47
47
  # @since 2.0.0
48
48
  class BulkInsert
49
49
  include Specifiable
50
+ include Idable
50
51
 
51
52
  # Execute the bulk insert operation.
52
53
  #
@@ -69,21 +70,22 @@ module Mongo
69
70
  private
70
71
 
71
72
  def execute_write_command(context)
72
- Result.new(Command::Insert.new(spec).execute(context))
73
+ command_spec = spec.merge(:documents => ensure_ids(documents))
74
+ Result.new(Command::Insert.new(command_spec).execute(context), @ids)
73
75
  end
74
76
 
75
77
  def execute_message(context)
76
78
  replies = []
77
79
  messages.map do |m|
78
80
  context.with_connection do |connection|
79
- result = LegacyResult.new(connection.dispatch([ m, gle ].compact))
81
+ result = LegacyResult.new(connection.dispatch([ m, gle ].compact), @ids)
80
82
  replies << result.reply
81
83
  if stop_sending?(result)
82
- return LegacyResult.new(replies)
84
+ return LegacyResult.new(replies, @ids)
83
85
  end
84
86
  end
85
87
  end
86
- LegacyResult.new(replies.compact.empty? ? nil : replies)
88
+ LegacyResult.new(replies.compact.empty? ? nil : replies, @ids)
87
89
  end
88
90
 
89
91
  def stop_sending?(result)
@@ -117,10 +119,10 @@ module Mongo
117
119
  def messages
118
120
  if ordered? || gle
119
121
  documents.collect do |doc|
120
- Protocol::Insert.new(db_name, coll_name, [ doc ], options)
122
+ Protocol::Insert.new(db_name, coll_name, ensure_ids([ doc ]), options)
121
123
  end
122
124
  else
123
- [ Protocol::Insert.new(db_name, coll_name, documents, { :flags => [:continue_on_error] }) ]
125
+ [ Protocol::Insert.new(db_name, coll_name, ensure_ids(documents), { :flags => [:continue_on_error] }) ]
124
126
  end
125
127
  end
126
128
  end
@@ -26,6 +26,25 @@ module Mongo
26
26
  class Result < Operation::Result
27
27
  include BulkMergable
28
28
 
29
+ # Get the ids of the inserted documents.
30
+ #
31
+ # @since 2.0.0
32
+ attr_reader :inserted_ids
33
+
34
+ # Initialize a new result.
35
+ #
36
+ # @example Instantiate the result.
37
+ # Result.new(replies, inserted_ids)
38
+ #
39
+ # @param [ Protocol::Reply ] replies The wire protocol replies.
40
+ # @params [ Array<Object> ] ids The ids of the inserted documents.
41
+ #
42
+ # @since 2.0.0
43
+ def initialize(replies, ids)
44
+ @replies = replies.is_a?(Protocol::Reply) ? [ replies ] : replies
45
+ @inserted_ids = ids
46
+ end
47
+
29
48
  # Gets the number of documents inserted.
30
49
  #
31
50
  # @example Get the number of documents inserted.
@@ -37,6 +56,18 @@ module Mongo
37
56
  def n_inserted
38
57
  written_count
39
58
  end
59
+
60
+ # Gets the id of the document inserted.
61
+ #
62
+ # @example Get id of the document inserted.
63
+ # result.inserted_id
64
+ #
65
+ # @return [ Object ] The id of the document inserted.
66
+ #
67
+ # @since 2.0.0
68
+ def inserted_id
69
+ inserted_ids.first
70
+ end
40
71
  end
41
72
 
42
73
  # Defines custom behaviour of results when inserting.
@@ -46,6 +77,25 @@ module Mongo
46
77
  class LegacyResult < Operation::Result
47
78
  include LegacyBulkMergable
48
79
 
80
+ # Get the ids of the inserted documents.
81
+ #
82
+ # @since 2.0.0
83
+ attr_reader :inserted_ids
84
+
85
+ # Initialize a new result.
86
+ #
87
+ # @example Instantiate the result.
88
+ # Result.new(replies, inserted_ids)
89
+ #
90
+ # @param [ Protocol::Reply ] replies The wire protocol replies.
91
+ # @params [ Array<Object> ] ids The ids of the inserted documents.
92
+ #
93
+ # @since 2.0.0
94
+ def initialize(replies, ids)
95
+ @replies = replies.is_a?(Protocol::Reply) ? [ replies ] : replies
96
+ @inserted_ids = ids
97
+ end
98
+
49
99
  # Gets the number of documents inserted.
50
100
  #
51
101
  # @example Get the number of documents inserted.
@@ -61,6 +111,18 @@ module Mongo
61
111
  n
62
112
  end
63
113
  end
114
+
115
+ # Gets the id of the document inserted.
116
+ #
117
+ # @example Get id of the document inserted.
118
+ # result.inserted_id
119
+ #
120
+ # @return [ Object ] The id of the document inserted.
121
+ #
122
+ # @since 2.0.0
123
+ def inserted_id
124
+ inserted_ids.first
125
+ end
64
126
  end
65
127
  end
66
128
  end
@@ -0,0 +1,41 @@
1
+ # Copyright (C) 2014-2015 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ module Operation
17
+ module Write
18
+ module Idable
19
+
20
+ private
21
+
22
+ def id(doc)
23
+ doc.respond_to?(:id) ? doc.id : (doc['_id'] || doc[:_id])
24
+ end
25
+
26
+ def has_id?(doc)
27
+ id(doc)
28
+ end
29
+
30
+ def ensure_ids(documents)
31
+ @ids ||= []
32
+ documents.collect do |doc|
33
+ doc_with_id = has_id?(doc) ? doc : doc.merge(_id: BSON::ObjectId.new)
34
+ @ids << id(doc_with_id)
35
+ doc_with_id
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -44,6 +44,7 @@ module Mongo
44
44
  class Insert
45
45
  include Executable
46
46
  include Specifiable
47
+ include Idable
47
48
 
48
49
  # Execute the insert operation.
49
50
  #
@@ -66,12 +67,13 @@ module Mongo
66
67
  private
67
68
 
68
69
  def execute_write_command(context)
69
- Result.new(Command::Insert.new(spec).execute(context)).validate!
70
+ command_spec = spec.merge(:documents => ensure_ids(documents))
71
+ Result.new(Command::Insert.new(command_spec).execute(context), @ids).validate!
70
72
  end
71
73
 
72
74
  def execute_message(context)
73
75
  context.with_connection do |connection|
74
- Result.new(connection.dispatch([ message, gle ].compact)).validate!
76
+ Result.new(connection.dispatch([ message, gle ].compact), @ids).validate!
75
77
  end
76
78
  end
77
79
 
@@ -82,7 +84,7 @@ module Mongo
82
84
 
83
85
  def message
84
86
  opts = !!options[:continue_on_error] ? { :flags => [:continue_on_error] } : {}
85
- Protocol::Insert.new(db_name, coll_name, documents, opts)
87
+ Protocol::Insert.new(db_name, coll_name, ensure_ids(documents), opts)
86
88
  end
87
89
  end
88
90
  end
@@ -25,6 +25,36 @@ module Mongo
25
25
  # @since 2.0.0
26
26
  class Result < Operation::Result
27
27
 
28
+ # Get the ids of the inserted documents.
29
+ #
30
+ # @since 2.0.0
31
+ attr_reader :inserted_ids
32
+
33
+ # Initialize a new result.
34
+ #
35
+ # @example Instantiate the result.
36
+ # Result.new(replies, inserted_ids)
37
+ #
38
+ # @param [ Protocol::Reply ] replies The wire protocol replies.
39
+ # @params [ Array<Object> ] ids The ids of the inserted documents.
40
+ #
41
+ # @since 2.0.0
42
+ def initialize(replies, ids)
43
+ @replies = replies.is_a?(Protocol::Reply) ? [ replies ] : replies
44
+ @inserted_ids = ids
45
+ end
46
+
47
+ # Gets the id of the document inserted.
48
+ #
49
+ # @example Get id of the document inserted.
50
+ # result.inserted_id
51
+ #
52
+ # @return [ Object ] The id of the document inserted.
53
+ #
54
+ # @since 2.0.0
55
+ def inserted_id
56
+ inserted_ids.first
57
+ end
28
58
  end
29
59
  end
30
60
  end
@@ -17,5 +17,5 @@ module Mongo
17
17
  # The current version of the driver.
18
18
  #
19
19
  # @since 2.0.0
20
- VERSION = '2.0.0'.freeze
20
+ VERSION = '2.0.1'.freeze
21
21
  end
@@ -294,6 +294,10 @@ describe Mongo::Collection do
294
294
  it 'inserts the documents into the collection', unless: write_command_enabled? do
295
295
  expect(result.written_count).to eq(0)
296
296
  end
297
+
298
+ it 'contains the ids in the result' do
299
+ expect(result.inserted_ids.size).to eq(2)
300
+ end
297
301
  end
298
302
 
299
303
  describe '#insert_one' do
@@ -313,6 +317,10 @@ describe Mongo::Collection do
313
317
  it 'inserts the document into the collection', unless: write_command_enabled? do
314
318
  expect(result.written_count).to eq(0)
315
319
  end
320
+
321
+ it 'contains the id in the result' do
322
+ expect(result.inserted_id).to_not be_nil
323
+ end
316
324
  end
317
325
 
318
326
  describe '#inspect' do
@@ -19,6 +19,10 @@ describe Mongo::Operation::Write::BulkInsert do
19
19
  described_class.new(spec)
20
20
  end
21
21
 
22
+ after do
23
+ authorized_collection.find.delete_many
24
+ end
25
+
22
26
  describe '#initialize' do
23
27
 
24
28
  context 'spec' do
@@ -81,6 +85,29 @@ describe Mongo::Operation::Write::BulkInsert do
81
85
  end
82
86
  end
83
87
 
88
+ describe 'document ids' do
89
+
90
+ context 'when documents do not contain an id' do
91
+
92
+ let(:documents) do
93
+ [{ 'field' => 'test' },
94
+ { 'field' => 'test' }]
95
+ end
96
+
97
+ let(:inserted_ids) do
98
+ op.execute(authorized_primary.context).inserted_ids
99
+ end
100
+
101
+ let(:collection_ids) do
102
+ authorized_collection.find(field: 'test').collect { |d| d['_id'] }
103
+ end
104
+
105
+ it 'adds an id to the documents' do
106
+ expect(inserted_ids).to eq(collection_ids)
107
+ end
108
+ end
109
+ end
110
+
84
111
  describe '#execute' do
85
112
 
86
113
  before do
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Mongo::Operation::Write::Command::Insert do
4
4
  include_context 'operation'
5
5
 
6
- let(:documents) { [{ :foo => 1 }] }
6
+ let(:documents) { [{ :_id => 1, :foo => 1 }] }
7
7
  let(:spec) do
8
8
  { :documents => documents,
9
9
  :db_name => db_name,
@@ -15,6 +15,10 @@ describe Mongo::Operation::Write::Insert do
15
15
  }
16
16
  end
17
17
 
18
+ after do
19
+ authorized_collection.find.delete_many
20
+ end
21
+
18
22
  let(:insert) do
19
23
  described_class.new(spec)
20
24
  end
@@ -80,6 +84,29 @@ describe Mongo::Operation::Write::Insert do
80
84
  end
81
85
  end
82
86
 
87
+ describe 'document ids' do
88
+
89
+ context 'when documents do not contain an id' do
90
+
91
+ let(:documents) do
92
+ [{ 'field' => 'test' },
93
+ { 'field' => 'test' }]
94
+ end
95
+
96
+ let(:inserted_ids) do
97
+ insert.execute(authorized_primary.context).inserted_ids
98
+ end
99
+
100
+ let(:collection_ids) do
101
+ authorized_collection.find(field: 'test').collect { |d| d['_id'] }
102
+ end
103
+
104
+ it 'adds an id to the documents' do
105
+ expect(inserted_ids).to eq(collection_ids)
106
+ end
107
+ end
108
+ end
109
+
83
110
  describe '#execute' do
84
111
 
85
112
  before do
@@ -121,15 +121,13 @@ module Mongo
121
121
  end
122
122
 
123
123
  def insert_many(collection)
124
- collection.insert_many(documents)
125
- # returning inserted_ids is optional
126
- { 'insertedIds' => documents.collect { |d| d['_id'] } }
124
+ result = collection.insert_many(documents)
125
+ { 'insertedIds' => result.inserted_ids }
127
126
  end
128
127
 
129
128
  def insert_one(collection)
130
129
  result = collection.insert_one(document)
131
- # returning inserted_id is optional
132
- { 'insertedId' => document['_id'] }
130
+ { 'insertedId' => result.inserted_id }
133
131
  end
134
132
 
135
133
  def update_return_doc(result)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Brock
@@ -32,7 +32,7 @@ cert_chain:
32
32
  o2UXDbWtz5PqoFd8EgNJAn3+BG1pwC9S9pVFG3WPucfAx/bE8iq/vvchHei5Y/Vo
33
33
  aAz5f/hY4zFeYWvGDBHYEXE1rTN2hhMSyJscPcFbmz0=
34
34
  -----END CERTIFICATE-----
35
- date: 2015-03-25 00:00:00.000000000 Z
35
+ date: 2015-03-26 00:00:00.000000000 Z
36
36
  dependencies:
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bson
@@ -186,6 +186,7 @@ files:
186
186
  - lib/mongo/operation/write/delete.rb
187
187
  - lib/mongo/operation/write/delete/result.rb
188
188
  - lib/mongo/operation/write/drop_index.rb
189
+ - lib/mongo/operation/write/idable.rb
189
190
  - lib/mongo/operation/write/insert.rb
190
191
  - lib/mongo/operation/write/insert/result.rb
191
192
  - lib/mongo/operation/write/remove_user.rb
@@ -293,9 +294,9 @@ files:
293
294
  - spec/mongo/operation/read/query_spec.rb
294
295
  - spec/mongo/operation/result_spec.rb
295
296
  - spec/mongo/operation/specifiable_spec.rb
296
- - spec/mongo/operation/write/bulk_delete_spec.rb
297
- - spec/mongo/operation/write/bulk_insert_spec.rb
298
- - spec/mongo/operation/write/bulk_update_spec.rb
297
+ - spec/mongo/operation/write/bulk/bulk_delete_spec.rb
298
+ - spec/mongo/operation/write/bulk/bulk_insert_spec.rb
299
+ - spec/mongo/operation/write/bulk/bulk_update_spec.rb
299
300
  - spec/mongo/operation/write/command/delete_spec.rb
300
301
  - spec/mongo/operation/write/command/insert_spec.rb
301
302
  - spec/mongo/operation/write/command/update_spec.rb
@@ -514,9 +515,9 @@ test_files:
514
515
  - spec/mongo/operation/read/query_spec.rb
515
516
  - spec/mongo/operation/result_spec.rb
516
517
  - spec/mongo/operation/specifiable_spec.rb
517
- - spec/mongo/operation/write/bulk_delete_spec.rb
518
- - spec/mongo/operation/write/bulk_insert_spec.rb
519
- - spec/mongo/operation/write/bulk_update_spec.rb
518
+ - spec/mongo/operation/write/bulk/bulk_delete_spec.rb
519
+ - spec/mongo/operation/write/bulk/bulk_insert_spec.rb
520
+ - spec/mongo/operation/write/bulk/bulk_update_spec.rb
520
521
  - spec/mongo/operation/write/command/delete_spec.rb
521
522
  - spec/mongo/operation/write/command/insert_spec.rb
522
523
  - spec/mongo/operation/write/command/update_spec.rb
metadata.gz.sig CHANGED
Binary file