You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bulkwriteoptions.go 1.9 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package options
  7. // DefaultOrdered is the default order for a BulkWriteOptions struct created from BulkWrite.
  8. var DefaultOrdered = true
  9. // BulkWriteOptions represent all possible options for a bulkWrite operation.
  10. type BulkWriteOptions struct {
  11. BypassDocumentValidation *bool // If true, allows the write to opt out of document-level validation.
  12. Ordered *bool // If true, when a write fails, return without performing remaining writes. Defaults to true.
  13. }
  14. // BulkWrite creates a new *BulkWriteOptions
  15. func BulkWrite() *BulkWriteOptions {
  16. return &BulkWriteOptions{
  17. Ordered: &DefaultOrdered,
  18. }
  19. }
  20. // SetOrdered configures the ordered option. If true, when a write fails, the function will return without attempting
  21. // remaining writes. Defaults to true.
  22. func (b *BulkWriteOptions) SetOrdered(ordered bool) *BulkWriteOptions {
  23. b.Ordered = &ordered
  24. return b
  25. }
  26. // SetBypassDocumentValidation specifies if the write should opt out of document-level validation.
  27. // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
  28. func (b *BulkWriteOptions) SetBypassDocumentValidation(bypass bool) *BulkWriteOptions {
  29. b.BypassDocumentValidation = &bypass
  30. return b
  31. }
  32. // MergeBulkWriteOptions combines the given *BulkWriteOptions into a single *BulkWriteOptions in a last one wins fashion.
  33. func MergeBulkWriteOptions(opts ...*BulkWriteOptions) *BulkWriteOptions {
  34. b := BulkWrite()
  35. for _, opt := range opts {
  36. if opt == nil {
  37. continue
  38. }
  39. if opt.Ordered != nil {
  40. b.Ordered = opt.Ordered
  41. }
  42. if opt.BypassDocumentValidation != nil {
  43. b.BypassDocumentValidation = opt.BypassDocumentValidation
  44. }
  45. }
  46. return b
  47. }