@@ -71,21 +71,22 @@ defmodule Mongo.Repo do
71
71
end
72
72
end
73
73
74
- def update ( % { __struct__: module , _id: id } = doc ) do
74
+ def update ( % { __struct__: module , _id: id } = doc , opts \\ [ ] ) do
75
75
collection = module . __collection__ ( :collection )
76
76
doc = module . timestamps ( doc )
77
77
78
- case Mongo . update_one ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , [ ] ) do
78
+ case Mongo . update_one ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , opts ) do
79
79
{ :error , reason } -> { :error , reason }
80
80
{ :ok , % { modified_count: _ } } -> { :ok , doc }
81
81
end
82
82
end
83
83
84
- def insert_or_update ( % { __struct__: module , _id: id } = doc ) do
84
+ def insert_or_update ( % { __struct__: module , _id: id } = doc , opts \\ [ ] ) do
85
85
collection = module . __collection__ ( :collection )
86
86
doc = module . timestamps ( doc )
87
+ opts = Keyword . put ( opts , :upsert , true )
87
88
88
- case Mongo . update_one ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , upsert: true ) do
89
+ case Mongo . update_one ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , opts ) do
89
90
{ :error , reason } -> { :error , reason }
90
91
{ :ok , % { upserted_ids: [ id ] } } -> { :ok , % { doc | _id: id } }
91
92
{ :ok , % { modified_count: _ } } -> { :ok , doc }
@@ -119,17 +120,18 @@ defmodule Mongo.Repo do
119
120
% { doc | _id: id }
120
121
end
121
122
122
- def update! ( % { __struct__: module , _id: id } = doc ) do
123
+ def update! ( % { __struct__: module , _id: id } = doc , opts \\ [ ] ) do
123
124
collection = module . __collection__ ( :collection )
124
125
doc = module . timestamps ( doc )
125
- Mongo . update_one! ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , [ ] )
126
+ Mongo . update_one! ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , opts )
126
127
doc
127
128
end
128
129
129
- def insert_or_update! ( % { __struct__: module , _id: id } = doc ) do
130
+ def insert_or_update! ( % { __struct__: module , _id: id } = doc , opts \\ [ ] ) do
130
131
collection = module . __collection__ ( :collection )
131
132
doc = module . timestamps ( doc )
132
- update_one_result = Mongo . update_one! ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , upsert: true )
133
+ opts = Keyword . put ( opts , :upsert , true )
134
+ update_one_result = Mongo . update_one! ( @ topology , collection , % { _id: id } , % { "$set" => module . dump ( doc ) } , opts )
133
135
134
136
case update_one_result do
135
137
% { upserted_ids: [ id ] } -> % { doc | _id: id }
@@ -230,8 +232,10 @@ defmodule Mongo.Repo do
230
232
Mongo . count_documents ( @ topology , collection , filter , opts )
231
233
end
232
234
233
- def exists? ( module , filter \\ % { } ) do
234
- with { :ok , count } <- count ( module , filter , limit: 1 ) do
235
+ def exists? ( module , filter \\ % { } , opts \\ [ ] ) do
236
+ opts = Keyword . put ( opts , :limit , 1 )
237
+
238
+ with { :ok , count } <- count ( module , filter , opts ) do
235
239
count > 0
236
240
end
237
241
end
@@ -243,7 +247,7 @@ defmodule Mongo.Repo do
243
247
"""
244
248
@ callback config ( ) :: Keyword . t ( )
245
249
246
- @ optional_callbacks get: 3 , get_by: 3 , aggregate: 3 , exists?: 2 , all: 3 , stream: 3 , update_all: 4 , delete_all: 3
250
+ @ optional_callbacks get: 3 , get_by: 3 , aggregate: 3 , exists?: 3 , all: 3 , stream: 3 , update_all: 4 , delete_all: 3
247
251
248
252
@ doc """
249
253
Returns a single document struct for the collection defined in the given module and bson object id.
@@ -348,7 +352,7 @@ defmodule Mongo.Repo do
348
352
349
353
MyApp.Repo.exists?(Post, %{title: title})
350
354
"""
351
- @ callback exists? ( module :: module ( ) , filter :: BSON . document ( ) ) :: boolean ( )
355
+ @ callback exists? ( module :: module ( ) , filter :: BSON . document ( ) , opts :: Keyword . t ( ) ) :: boolean ( )
352
356
353
357
@ doc """
354
358
Applies the updates for the documents in the given collection module and filter.
@@ -417,7 +421,7 @@ defmodule Mongo.Repo do
417
421
@ callback fetch_by ( module :: module ( ) , query :: BSON . document ( ) , opts :: Keyword . t ( ) ) ::
418
422
{ :ok , Mongo.Collection . t ( ) } | { :error , :not_found } | { :error , any ( ) }
419
423
420
- @ optional_callbacks insert: 2 , insert!: 2 , update: 1 , update!: 1 , insert_or_update: 1 , insert_or_update!: 1 , delete: 2 , delete!: 2 , insert_all: 3
424
+ @ optional_callbacks insert: 2 , insert!: 2 , update: 2 , update!: 2 , insert_or_update: 2 , insert_or_update!: 2 , delete: 2 , delete!: 2 , insert_all: 3
421
425
422
426
@ doc """
423
427
Inserts a new document struct into the database and returns a `{:ok, doc}` tuple.
@@ -443,12 +447,12 @@ defmodule Mongo.Repo do
443
447
post = MyApp.Repo.insert!(Post.new())
444
448
MyApp.Repo.update(%{post | title: "new"})
445
449
"""
446
- @ callback update ( doc :: Mongo.Collection . t ( ) ) :: { :ok , Mongo.Collection . t ( ) } | { :error , any ( ) }
450
+ @ callback update ( doc :: Mongo.Collection . t ( ) , opts :: Keyword . t ( ) ) :: { :ok , Mongo.Collection . t ( ) } | { :error , any ( ) }
447
451
448
452
@ doc """
449
453
Same as `Mongo.Repo.update/1` but raises an error.
450
454
"""
451
- @ callback update! ( doc :: Mongo.Collection . t ( ) ) :: Mongo.Collection . t ( )
455
+ @ callback update! ( doc :: Mongo.Collection . t ( ) , opts :: Keyword . t ( ) ) :: Mongo.Collection . t ( )
452
456
453
457
@ doc """
454
458
Upserts a document struct and returns a `{:ok, doc}` tuple.
@@ -457,12 +461,12 @@ defmodule Mongo.Repo do
457
461
458
462
MyApp.Repo.insert_or_update(Post.new())
459
463
"""
460
- @ callback insert_or_update ( doc :: Mongo.Collection . t ( ) ) :: { :ok , Mongo.Collection . t ( ) } | { :error , any ( ) }
464
+ @ callback insert_or_update ( doc :: Mongo.Collection . t ( ) , opts :: Keyword . t ( ) ) :: { :ok , Mongo.Collection . t ( ) } | { :error , any ( ) }
461
465
462
466
@ doc """
463
467
Same as `Mongo.Repo.insert_or_update/1` but raises an error.
464
468
"""
465
- @ callback insert_or_update! ( doc :: Mongo.Collection . t ( ) ) :: Mongo.Collection . t ( )
469
+ @ callback insert_or_update! ( doc :: Mongo.Collection . t ( ) , opts :: Keyword . t ( ) ) :: Mongo.Collection . t ( )
466
470
467
471
@ doc """
468
472
Deletes a document struct from the database and returns a `{:ok, doc}` tuple.
0 commit comments