Update Documents

This page provides examples in:

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • PHP
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

This page uses the following mongo shell methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses MongoDB Compass toupdate the documents.

The examples on this page use the inventory collection.Populate the inventory collection with the followingdocuments:

This page uses thefollowing PyMongoPython driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing Java Synchronous Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB Node.js Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB PHP Library methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing Motordriver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing Java Reactive Streams Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB C# Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB Perl Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB Ruby Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB Scala Driver methods:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

This page uses thefollowing MongoDB Go Driver functions:

The examples on this page use theinventory collection. To create and/or populate theinventory collection, run the following:

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • PHP
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go
  1. db.inventory.insertMany( [
  2. { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
  3. { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
  4. { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
  5. { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
  6. { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
  7. { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
  8. { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
  9. { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
  10. { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
  11. { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
  12. ] );

You can run the operation in the web shell below:

  1. [
  2. { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
  3. { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
  4. { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
  5. { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
  6. { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
  7. { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
  8. { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
  9. { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
  10. { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
  11. { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
  12. ]

For instructions on inserting documents using MongoDB Compass, seeInsert Documents.

  1. db.inventory.insert_many([
  2. {"item": "canvas",
  3. "qty": 100,
  4. "size": {"h": 28, "w": 35.5, "uom": "cm"},
  5. "status": "A"},
  6. {"item": "journal",
  7. "qty": 25,
  8. "size": {"h": 14, "w": 21, "uom": "cm"},
  9. "status": "A"},
  10. {"item": "mat",
  11. "qty": 85,
  12. "size": {"h": 27.9, "w": 35.5, "uom": "cm"},
  13. "status": "A"},
  14. {"item": "mousepad",
  15. "qty": 25,
  16. "size": {"h": 19, "w": 22.85, "uom": "cm"},
  17. "status": "P"},
  18. {"item": "notebook",
  19. "qty": 50,
  20. "size": {"h": 8.5, "w": 11, "uom": "in"},
  21. "status": "P"},
  22. {"item": "paper",
  23. "qty": 100,
  24. "size": {"h": 8.5, "w": 11, "uom": "in"},
  25. "status": "D"},
  26. {"item": "planner",
  27. "qty": 75,
  28. "size": {"h": 22.85, "w": 30, "uom": "cm"},
  29. "status": "D"},
  30. {"item": "postcard",
  31. "qty": 45,
  32. "size": {"h": 10, "w": 15.25, "uom": "cm"},
  33. "status": "A"},
  34. {"item": "sketchbook",
  35. "qty": 80,
  36. "size": {"h": 14, "w": 21, "uom": "cm"},
  37. "status": "A"},
  38. {"item": "sketch pad",
  39. "qty": 95,
  40. "size": {"h": 22.85, "w": 30.5, "uom": "cm"},
  41. "status": "A"}])
  1. collection.insertMany(asList(
  2. Document.parse("{ item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }"),
  3. Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
  4. Document.parse("{ item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }"),
  5. Document.parse("{ item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }"),
  6. Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }"),
  7. Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
  8. Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
  9. Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }"),
  10. Document.parse("{ item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
  11. Document.parse("{ item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' }")
  12. ));
  1. await db.collection('inventory').insertMany([
  2. {
  3. item: 'canvas',
  4. qty: 100,
  5. size: { h: 28, w: 35.5, uom: 'cm' },
  6. status: 'A'
  7. },
  8. {
  9. item: 'journal',
  10. qty: 25,
  11. size: { h: 14, w: 21, uom: 'cm' },
  12. status: 'A'
  13. },
  14. {
  15. item: 'mat',
  16. qty: 85,
  17. size: { h: 27.9, w: 35.5, uom: 'cm' },
  18. status: 'A'
  19. },
  20. {
  21. item: 'mousepad',
  22. qty: 25,
  23. size: { h: 19, w: 22.85, uom: 'cm' },
  24. status: 'P'
  25. },
  26. {
  27. item: 'notebook',
  28. qty: 50,
  29. size: { h: 8.5, w: 11, uom: 'in' },
  30. status: 'P'
  31. },
  32. {
  33. item: 'paper',
  34. qty: 100,
  35. size: { h: 8.5, w: 11, uom: 'in' },
  36. status: 'D'
  37. },
  38. {
  39. item: 'planner',
  40. qty: 75,
  41. size: { h: 22.85, w: 30, uom: 'cm' },
  42. status: 'D'
  43. },
  44. {
  45. item: 'postcard',
  46. qty: 45,
  47. size: { h: 10, w: 15.25, uom: 'cm' },
  48. status: 'A'
  49. },
  50. {
  51. item: 'sketchbook',
  52. qty: 80,
  53. size: { h: 14, w: 21, uom: 'cm' },
  54. status: 'A'
  55. },
  56. {
  57. item: 'sketch pad',
  58. qty: 95,
  59. size: { h: 22.85, w: 30.5, uom: 'cm' },
  60. status: 'A'
  61. }
  62. ]);
  1. $insertManyResult = $db->inventory->insertMany([
  2. [
  3. 'item' => 'canvas',
  4. 'qty' => 100,
  5. 'size' => ['h' => 28, 'w' => 35.5, 'uom' => 'cm'],
  6. 'status' => 'A',
  7. ],
  8. [
  9. 'item' => 'journal',
  10. 'qty' => 25,
  11. 'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
  12. 'status' => 'A',
  13. ],
  14. [
  15. 'item' => 'mat',
  16. 'qty' => 85,
  17. 'size' => ['h' => 27.9, 'w' => 35.5, 'uom' => 'cm'],
  18. 'status' => 'A',
  19. ],
  20. [
  21. 'item' => 'mousepad',
  22. 'qty' => 25,
  23. 'size' => ['h' => 19, 'w' => 22.85, 'uom' => 'cm'],
  24. 'status' => 'P',
  25. ],
  26. [
  27. 'item' => 'notebook',
  28. 'qty' => 50,
  29. 'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
  30. 'status' => 'P',
  31. ],
  32. [
  33. 'item' => 'paper',
  34. 'qty' => 100,
  35. 'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
  36. 'status' => 'D',
  37. ],
  38. [
  39. 'item' => 'planner',
  40. 'qty' => 75,
  41. 'size' => ['h' => 22.85, 'w' => 30, 'uom' => 'cm'],
  42. 'status' => 'D',
  43. ],
  44. [
  45. 'item' => 'postcard',
  46. 'qty' => 45,
  47. 'size' => ['h' => 10, 'w' => 15.25, 'uom' => 'cm'],
  48. 'status' => 'A',
  49. ],
  50. [
  51. 'item' => 'sketchbook',
  52. 'qty' => 80,
  53. 'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
  54. 'status' => 'A',
  55. ],
  56. [
  57. 'item' => 'sketch pad',
  58. 'qty' => 95,
  59. 'size' => ['h' => 22.85, 'w' => 30.5, 'uom' => 'cm'],
  60. 'status' => 'A',
  61. ],
  62. ]);
  1. await db.inventory.insert_many([
  2. {"item": "canvas",
  3. "qty": 100,
  4. "size": {"h": 28, "w": 35.5, "uom": "cm"},
  5. "status": "A"},
  6. {"item": "journal",
  7. "qty": 25,
  8. "size": {"h": 14, "w": 21, "uom": "cm"},
  9. "status": "A"},
  10. {"item": "mat",
  11. "qty": 85,
  12. "size": {"h": 27.9, "w": 35.5, "uom": "cm"},
  13. "status": "A"},
  14. {"item": "mousepad",
  15. "qty": 25,
  16. "size": {"h": 19, "w": 22.85, "uom": "cm"},
  17. "status": "P"},
  18. {"item": "notebook",
  19. "qty": 50,
  20. "size": {"h": 8.5, "w": 11, "uom": "in"},
  21. "status": "P"},
  22. {"item": "paper",
  23. "qty": 100,
  24. "size": {"h": 8.5, "w": 11, "uom": "in"},
  25. "status": "D"},
  26. {"item": "planner",
  27. "qty": 75,
  28. "size": {"h": 22.85, "w": 30, "uom": "cm"},
  29. "status": "D"},
  30. {"item": "postcard",
  31. "qty": 45,
  32. "size": {"h": 10, "w": 15.25, "uom": "cm"},
  33. "status": "A"},
  34. {"item": "sketchbook",
  35. "qty": 80,
  36. "size": {"h": 14, "w": 21, "uom": "cm"},
  37. "status": "A"},
  38. {"item": "sketch pad",
  39. "qty": 95,
  40. "size": {"h": 22.85, "w": 30.5, "uom": "cm"},
  41. "status": "A"}])
  1. Publisher<Success> insertManyPublisher = collection.insertMany(asList(
  2. Document.parse("{ item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }"),
  3. Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
  4. Document.parse("{ item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }"),
  5. Document.parse("{ item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }"),
  6. Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }"),
  7. Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
  8. Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
  9. Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }"),
  10. Document.parse("{ item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
  11. Document.parse("{ item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' }")
  12. ));
  1. var documents = new[]
  2. {
  3. new BsonDocument
  4. {
  5. { "item", "canvas" },
  6. { "qty", 100 },
  7. { "size", new BsonDocument { { "h", 28 }, { "w", 35.5 }, { "uom", "cm" } } },
  8. { "status", "A" }
  9. },
  10. new BsonDocument
  11. {
  12. { "item", "journal" },
  13. { "qty", 25 },
  14. { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } },
  15. { "status", "A" }
  16. },
  17. new BsonDocument
  18. {
  19. { "item", "mat" },
  20. { "qty", 85 },
  21. { "size", new BsonDocument { { "h", 27.9 }, { "w", 35.5 }, { "uom", "cm" } } },
  22. { "status", "A" }
  23. },
  24. new BsonDocument
  25. {
  26. { "item", "mousepad" },
  27. { "qty", 25 },
  28. { "size", new BsonDocument { { "h", 19 }, { "w", 22.85 }, { "uom", "cm" } } },
  29. { "status", "P" }
  30. },
  31. new BsonDocument
  32. {
  33. { "item", "notebook" },
  34. { "qty", 50 },
  35. { "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
  36. { "status", "P" } },
  37. new BsonDocument
  38. {
  39. { "item", "paper" },
  40. { "qty", 100 },
  41. { "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
  42. { "status", "D" }
  43. },
  44. new BsonDocument
  45. {
  46. { "item", "planner" },
  47. { "qty", 75 },
  48. { "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm" } } },
  49. { "status", "D" }
  50. },
  51. new BsonDocument
  52. {
  53. { "item", "postcard" },
  54. { "qty", 45 },
  55. { "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm" } } },
  56. { "status", "A" }
  57. },
  58. new BsonDocument
  59. {
  60. { "item", "sketchbook" },
  61. { "qty", 80 },
  62. { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } },
  63. { "status", "A" }
  64. },
  65. new BsonDocument
  66. {
  67. { "item", "sketch pad" },
  68. { "qty", 95 },
  69. { "size", new BsonDocument { { "h", 22.85 }, { "w", 30.5 }, { "uom", "cm" } } }, { "status", "A" } },
  70. };
  71. collection.InsertMany(documents);
  1. $db->coll("inventory")->insert_many(
  2. [
  3. {
  4. item => "canvas",
  5. qty => 100,
  6. size => { h => 28, w => 35.5, uom => "cm" },
  7. status => "A"
  8. },
  9. {
  10. item => "journal",
  11. qty => 25,
  12. size => { h => 14, w => 21, uom => "cm" },
  13. status => "A"
  14. },
  15. {
  16. item => "mat",
  17. qty => 85,
  18. size => { h => 27.9, w => 35.5, uom => "cm" },
  19. status => "A"
  20. },
  21. {
  22. item => "mousepad",
  23. qty => 25,
  24. size => { h => 19, w => 22.85, uom => "cm" },
  25. status => "P"
  26. },
  27. {
  28. item => "notebook",
  29. qty => 50,
  30. size => { h => 8.5, w => 11, uom => "in" },
  31. status => "P"
  32. },
  33. {
  34. item => "paper",
  35. qty => 100,
  36. size => { h => 8.5, w => 11, uom => "in" },
  37. status => "D"
  38. },
  39. {
  40. item => "planner",
  41. qty => 75,
  42. size => { h => 22.85, w => 30, uom => "cm" },
  43. status => "D"
  44. },
  45. {
  46. item => "postcard",
  47. qty => 45,
  48. size => { h => 10, w => 15.25, uom => "cm" },
  49. status => "A"
  50. },
  51. {
  52. item => "sketchbook",
  53. qty => 80,
  54. size => { h => 14, w => 21, uom => "cm" },
  55. status => "A"
  56. },
  57. {
  58. item => "sketch pad",
  59. qty => 95,
  60. size => { h => 22.85, w => 30.5, uom => "cm" },
  61. status => "A"
  62. }
  63. ]
  64. );
  1. client[:inventory].insert_many([
  2. { item: 'canvas',
  3. qty: 100,
  4. size: { h: 28, w: 35.5, uom: 'cm' },
  5. status: 'A' },
  6. { item: 'journal',
  7. qty: 25,
  8. size: { h: 14, w: 21, uom: 'cm' },
  9. status: 'A' },
  10. { item: 'mat',
  11. qty: 85,
  12. size: { h: 27.9, w: 35.5, uom: 'cm' },
  13. status: 'A' },
  14. { item: 'mousepad',
  15. qty: 25,
  16. size: { h: 19, w: 22.85, uom: 'cm' },
  17. status: 'P' },
  18. { item: 'notebook',
  19. qty: 50,
  20. size: { h: 8.5, w: 11, uom: 'in' },
  21. status: 'P' },
  22. { item: 'paper',
  23. qty: 100,
  24. size: { h: 8.5, w: 11, uom: 'in' },
  25. status: 'D' },
  26. { item: 'planner',
  27. qty: 75,
  28. size: { h: 22.85, w: 30, uom: 'cm' },
  29. status: 'D' },
  30. { item: 'postcard',
  31. qty: 45,
  32. size: { h: 10, w: 15.25, uom: 'cm' },
  33. status: 'A' },
  34. { item: 'sketchbook',
  35. qty: 80,
  36. size: { h: 14, w: 21, uom: 'cm' },
  37. status: 'A' },
  38. { item: 'sketch pad',
  39. qty: 95,
  40. size: { h: 22.85, w: 30.5, uom: 'cm' },
  41. status: 'A' }
  42. ])
  1. collection.insertMany(Seq(
  2. Document("""{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" }"""),
  3. Document("""{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""),
  4. Document("""{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" }"""),
  5. Document("""{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" }"""),
  6. Document("""{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" }"""),
  7. Document("""{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }"""),
  8. Document("""{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }"""),
  9. Document("""{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }"""),
  10. Document("""{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""),
  11. Document("""{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }""")
  12. )).execute()
  1. docs := []interface{}{
  2. bson.D{
  3. {"item", "canvas"},
  4. {"qty", 100},
  5. {"size", bson.D{
  6. {"h", 28},
  7. {"w", 35.5},
  8. {"uom", "cm"},
  9. }},
  10. {"status", "A"},
  11. },
  12. bson.D{
  13. {"item", "journal"},
  14. {"qty", 25},
  15. {"size", bson.D{
  16. {"h", 14},
  17. {"w", 21},
  18. {"uom", "cm"},
  19. }},
  20. {"status", "A"},
  21. },
  22. bson.D{
  23. {"item", "mat"},
  24. {"qty", 85},
  25. {"size", bson.D{
  26. {"h", 27.9},
  27. {"w", 35.5},
  28. {"uom", "cm"},
  29. }},
  30. {"status", "A"},
  31. },
  32. bson.D{
  33. {"item", "mousepad"},
  34. {"qty", 25},
  35. {"size", bson.D{
  36. {"h", 19},
  37. {"w", 22.85},
  38. {"uom", "in"},
  39. }},
  40. {"status", "P"},
  41. },
  42. bson.D{
  43. {"item", "notebook"},
  44. {"qty", 50},
  45. {"size", bson.D{
  46. {"h", 8.5},
  47. {"w", 11},
  48. {"uom", "in"},
  49. }},
  50. {"status", "P"},
  51. },
  52. bson.D{
  53. {"item", "paper"},
  54. {"qty", 100},
  55. {"size", bson.D{
  56. {"h", 8.5},
  57. {"w", 11},
  58. {"uom", "in"},
  59. }},
  60. {"status", "D"},
  61. },
  62. bson.D{
  63. {"item", "planner"},
  64. {"qty", 75},
  65. {"size", bson.D{
  66. {"h", 22.85},
  67. {"w", 30},
  68. {"uom", "cm"},
  69. }},
  70. {"status", "D"},
  71. },
  72. bson.D{
  73. {"item", "postcard"},
  74. {"qty", 45},
  75. {"size", bson.D{
  76. {"h", 10},
  77. {"w", 15.25},
  78. {"uom", "cm"},
  79. }},
  80. {"status", "A"},
  81. },
  82. bson.D{
  83. {"item", "sketchbook"},
  84. {"qty", 80},
  85. {"size", bson.D{
  86. {"h", 14},
  87. {"w", 21},
  88. {"uom", "cm"},
  89. }},
  90. {"status", "A"},
  91. },
  92. bson.D{
  93. {"item", "sketch pad"},
  94. {"qty", 95},
  95. {"size", bson.D{
  96. {"h", 22.85},
  97. {"w", 30.5},
  98. {"uom", "cm"},
  99. }},
  100. {"status", "A"},
  101. },
  102. }
  103.  
  104. result, err := coll.InsertMany(context.Background(), docs)

Update Documents in a Collection

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • PHP
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

To update a document, MongoDB providesupdate operators, suchas $set, to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator>: { <field1>: <value1>, ... },
  3. <update operator>: { <field2>: <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document in Compass, hover over the targetdocument and click the pencil icon:

Click edit document

After clicking the pencil icon, the document enters edit mode:

Document edit mode

You can now change the this document by clicking the item you wish tochange and modifying the value.

For detailed instructions on updating documents in Compass,refer to theCompass documentationor follow the example below.

Once you are satisfied with your changes, clickUpdate to save the updated document.

Click Cancel to revert any modifications made tothe document and exit edit mode.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator>: { <field1>: <value1>, ... },
  3. <update operator>: { <field2>: <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

The driver provides the com.mongodb.client.model.Updatesclass to facilitate the creation of update documents. For example:

  1. combine(set( <field1>, <value1>), set(<field2>, <value2> ) )

For a list of the update helpers, see com.mongodb.client.model.Updates.

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator>: { <field1>: <value1>, ... },
  3. <update operator>: { <field2>: <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. [
  2. <update operator> => [ <field1> => <value1>, ... ],
  3. <update operator> => [ <field2> => <value2>, ... ],
  4. ...
  5. ]

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator>: { <field1>: <value1>, ... },
  3. <update operator>: { <field2>: <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

The driver provides the com.mongodb.client.model.Updatesclass to facilitate the creation of update documents. For example:

  1. combine(set( <field1>, <value1>), set(<field2>, <value2> ) )

For a list of the update helpers, see com.mongodb.client.model.Updates.

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator> => { <field1> => <value1>, ... },
  3. <update operator> => { <field2> => <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator> => { <field1> => <value1>, ... },
  3. <update operator> => { <field2> => <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. {
  2. <update operator> => { <field1> => <value1>, ... },
  3. <update operator> => { <field2> => <value2>, ... },
  4. ...
  5. }

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

To use the update operators, pass to the update methods anupdate document of the form:

  1. (
  2. set (<field1>, <value1>),
  3. set (<field2>, <value2>),
  4. ...
  5. )

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

To update a document, MongoDB providesupdate operators suchas $set to modify field values.

Some update operators, such as $set, will create the field ifthe field does not exist. See the individualupdate operator reference fordetails.

Note

Starting in MongoDB 4.2, MongoDB can accept an aggregation pipelineto specify the modifications to make instead of an update document.See the method reference page for details.

Update a Single Document

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • PHP
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

The following example uses thedb.collection.updateOne() method on theinventory collection to update the first document whereitem equals "paper":

The following example demonstrates using MongoDB Compass to modifya single document where item: paper in the inventorycollection:

Note

This example uses the CompassTable View to modify thedocument. The editing process using the CompassList View follows a verysimilar approach.

For more information on the differences between Table Viewand List View in Compass, refer to theCompass documentation.

The following example uses theupdate_one() method onthe inventory collection to update the first documentwhere item equals "paper":

The following example uses thecom.mongodb.client.MongoCollection.updateOne method on theinventory collection to update the first document whereitem equals "paper":

The following example uses theCollection.updateOne()method on the inventory collection to update the _first_document where item equals "paper":

The following example uses theupdateOne()method on the inventory collection to update the _first_document where item equals "paper":

The following example uses theupdate_one() method onthe inventory collection to update the first documentwhere item equals "paper":

The following example uses thecom.mongodb.reactivestreams.client.MongoCollection.updateOne)on the inventory collection to update the first document whereitem equals "paper":

The following example uses theIMongoCollection.UpdateOne() method on theinventory collection to update the first document whereitem equals "paper":

The following example uses theupdate_one() method on theinventory collection to update the first document whereitem equals "paper":

The following example uses theupdate_one() method on theinventory collection to update the first document whereitem equals "paper":

The following example uses theupdateOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult])method on the inventory collection to update the _first_document where item equals "paper":

The following example uses theCollection.UpdateOne method on theinventory collection to update the first document whereitem equals "paper":

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • PHP
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go
  1. db.inventory.updateOne(
  2. { item: "paper" },
  3. {
  4. $set: { "size.uom": "cm", status: "P" },
  5. $currentDate: { lastModified: true }
  6. }
  7. )

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.

Modify the target document as follows:

  • Change the status field from D to P.
  • Change the size.uom field from in to cm.
  • Add a new field called lastModified whose value willbe today’s date.

  • Click the Table button in the top navigationto access the Table View:

Access Table View

  • Use the Compass query bar tolocate the target document.

Copy the following filter document into the query bar and clickFind:

  1. { item: "paper" }

Find Paper document

  • Hover over the status field and click the pencil iconwhich appears on the right side of the document to enteredit mode:

Click edit button

  • Change the value of the field to "P".

  • Click the Update button below the field tosave your changes.

  • Hover over the size field and click theoutward-pointing arrows which appear on the right side ofthe field. This opens a new tab which displays the fieldswithin the size object:

Expand size object

  • Using the same process outlined in steps 3-5 for editing thestatus field, change the value of the size.uom fieldto "cm".

  • Click the left-most tab above the table labelledinventory to return to the original table view, whichdisplays the top-level document:

Click inventory tab

  • Hover over the status field and click the pencil iconwhich appears on the right side of the document to re-enteredit mode.

  • Click inside of the status field and click theplus button icon which appears in the edit menu.

Click the Add Field After status button whichappears below the plus button:

Add field after status

  • Add a new field called lastModified with a value oftoday’s date. Set the field type to Date:

Submit update

  • Click the Update button below the field tosave your changes.

Note

Because MongoDB Compass does not support$currentDate or any otherField Update Operators,you must manually enter the date value in Compass.

  1. db.inventory.update_one(
  2. {"item": "paper"},
  3. {"$set": {"size.uom": "cm", "status": "P"},
  4. "$currentDate": {"lastModified": True}})
  1. collection.updateOne(eq("item", "paper"),
  2. combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified")));

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. await db.collection('inventory').updateOne(
  2. { item: 'paper' },
  3. {
  4. $set: { 'size.uom': 'cm', status: 'P' },
  5. $currentDate: { lastModified: true }
  6. }
  7. );

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. $updateResult = $db->inventory->updateOne(
  2. ['item' => 'paper'],
  3. [
  4. '$set' => ['size.uom' => 'cm', 'status' => 'P'],
  5. '$currentDate' => ['lastModified' => true],
  6. ]
  7. );

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. await db.inventory.update_one(
  2. {"item": "paper"},
  3. {"$set": {"size.uom": "cm", "status": "P"},
  4. "$currentDate": {"lastModified": True}})

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. Publisher<UpdateResult> updateOnePublisher = collection.updateOne(eq("item", "paper"),
  2. combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified")));

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. var filter = Builders<BsonDocument>.Filter.Eq("item", "paper");
  2. var update = Builders<BsonDocument>.Update.Set("size.uom", "cm").Set("status", "P").CurrentDate("lastModified");
  3. var result = collection.UpdateOne(filter, update);

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. # For boolean values, use boolean.pm for 'true' and 'false'
  2. $db->coll("inventory")->update_one(
  3. { item => "paper" },
  4. {
  5. '$set' => { "size.uom" => "cm", status => "P" },
  6. '$currentDate' => { lastModified => true }
  7. }
  8. );

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. client[:inventory].update_one({ item: 'paper'},
  2. { '$set' => { 'size.uom' => 'cm', 'status' => 'P' },
  3. '$currentDate' => { 'lastModified' => true } })

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. collection.updateOne(equal("item", "paper"),
  2. combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified"))
  3. ).execute()

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. result, err := coll.UpdateOne(
  2. context.Background(),
  3. bson.D{
  4. {"item", "paper"},
  5. },
  6. bson.D{
  7. {"$set", bson.D{
  8. {"size.uom", "cm"},
  9. {"status", "P"},
  10. }},
  11. {"$currentDate", bson.D{
  12. {"lastModified", true},
  13. }},
  14. },
  15. )

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "cm" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  • Mongo Shell
  • Python
  • Java (Sync)
  • Node.js
  • PHP
  • Other
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

Update Multiple Documents

New in version 3.2.

The following example uses the db.collection.updateMany()method on the inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

New in version 3.2.

The following example uses theupdate_many() method onthe inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

The following example uses thecom.mongodb.client.MongoCollection.updateMany method onthe inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

The following example uses theCollection.updateMany()method on the inventory collection to update all documentswhere qty is less than 50:

Update Multiple Documents

The following example uses theupdateMany()method on the inventory collection to update all documentswhere qty is less than 50:

Update Multiple Documents

New in version 3.2.

The following example uses theupdate_many()method on the inventory collection to update all documentswhere qty is less than 50:

Update Multiple Documents

The following example uses thecom.mongodb.reactivestreams.client.MongoCollection.updateMany)method on the inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

The following example uses theIMongoCollection.UpdateMany()method on the inventory collection to update all documentswhere qty is less than 50:

Update Multiple Documents

The following example uses theupdate_many() method onthe inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

The following example uses theupdate_many() method onthe inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

The following example uses theupdateMany():org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]) method onthe inventory collection to update all documents whereqty is less than 50:

Update Multiple Documents

The following example uses theCollection.UpdateManymethod on the inventory collection to update all documentswhere qty is less than 50:

  • Mongo Shell
  • Python
  • Java (Sync)
  • Node.js
  • PHP
  • Other
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go
  1. db.inventory.updateMany(
  2. { "qty": { $lt: 50 } },
  3. {
  4. $set: { "size.uom": "in", status: "P" },
  5. $currentDate: { lastModified: true }
  6. }
  7. )

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. db.inventory.update_many(
  2. {"qty": {"$lt": 50}},
  3. {"$set": {"size.uom": "in", "status": "P"},
  4. "$currentDate": {"lastModified": True}})
  1. collection.updateMany(lt("qty", 50),
  2. combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified")));

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. await db.collection('inventory').updateMany(
  2. { qty: { $lt: 50 } },
  3. {
  4. $set: { 'size.uom': 'in', status: 'P' },
  5. $currentDate: { lastModified: true }
  6. }
  7. );

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. $updateResult = $db->inventory->updateMany(
  2. ['qty' => ['$lt' => 50]],
  3. [
  4. '$set' => ['size.uom' => 'cm', 'status' => 'P'],
  5. '$currentDate' => ['lastModified' => true],
  6. ]
  7. );

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. await db.inventory.update_many(
  2. {"qty": {"$lt": 50}},
  3. {"$set": {"size.uom": "in", "status": "P"},
  4. "$currentDate": {"lastModified": True}})

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. Publisher<UpdateResult> updateManyPublisher = collection.updateMany(lt("qty", 50),
  2. combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified")));

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. var filter = Builders<BsonDocument>.Filter.Lt("qty", 50);
  2. var update = Builders<BsonDocument>.Update.Set("size.uom", "in").Set("status", "P").CurrentDate("lastModified");
  3. var result = collection.UpdateMany(filter, update);

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. # For boolean values, use boolean.pm for 'true' and 'false'
  2. $db->coll("inventory")->update_many(
  3. { qty => { '$lt' => 50 } },
  4. {
  5. '$set' => { "size.uom" => "in", status => "P" },
  6. '$currentDate' => { lastModified => true }
  7. }
  8. );

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. client[:inventory].update_many({ qty: { '$lt' => 50 } },
  2. { '$set' => { 'size.uom' => 'in', 'status' => 'P' },
  3. '$currentDate' => { 'lastModified' => true } })

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. collection.updateMany(lt("qty", 50),
  2. combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified"))
  3. ).execute()

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  1. result, err := coll.UpdateMany(
  2. context.Background(),
  3. bson.D{
  4. {"qty", bson.D{
  5. {"$lt", 50},
  6. }},
  7. },
  8. bson.D{
  9. {"$set", bson.D{
  10. {"size.uom", "cm"},
  11. {"status", "P"},
  12. }},
  13. {"$currentDate", bson.D{
  14. {"lastModified", true},
  15. }},
  16. },
  17. )

The update operation:

  • uses the $set operator to update the value of thesize.uom field to "in" and the value of the statusfield to "P",
  • uses the $currentDate operator to update the valueof the lastModified field to the current date. IflastModified field does not exist,$currentDate will create the field. See$currentDate for details.
  • Mongo Shell
  • Python
  • Java (Sync)
  • Node.js
  • PHP
  • Other
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument todb.collection.replaceOne().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toreplace_one().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument tocom.mongodb.client.MongoCollection.replaceOne.

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the_id field, pass an entirely new document as the secondargument toCollection.replaceOne().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toreplaceOne().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toreplace_one().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument tocom.mongodb.reactivestreams.client.MongoCollection.replaceOne).

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toIMongoCollection.ReplaceOne().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toreplace_one().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toreplace_one().

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toreplaceOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult])

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

Replace a Document

To replace the entire content of a document except for the _idfield, pass an entirely new document as the second argument toCollection.ReplaceOne.

When replacing a document, the replacement document must consist of onlyfield/value pairs; i.e. do not include update operators expressions.

The replacement document can have different fields from the originaldocument. In the replacement document, you can omit the _id fieldsince the _id field is immutable; however, if you do include the_id field, it must have the same value as the current value.

The following example replaces the first document from theinventory collection where item: "paper":

  • Mongo Shell
  • Python
  • Java (Sync)
  • Node.js
  • PHP
  • Other
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go
  1. db.inventory.replaceOne(
  2. { item: "paper" },
  3. { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
  4. )
  1. db.inventory.replace_one(
  2. {"item": "paper"},
  3. {"item": "paper",
  4. "instock": [
  5. {"warehouse": "A", "qty": 60},
  6. {"warehouse": "B", "qty": 40}]})
  1. collection.replaceOne(eq("item", "paper"),
  2. Document.parse("{ item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] }"));
  1. await db.collection('inventory').replaceOne(
  2. { item: 'paper' },
  3. {
  4. $set: {
  5. item: 'paper',
  6. instock: [{ warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 }]
  7. },
  8. $unset: {
  9. qty: '',
  10. size: '',
  11. status: '',
  12. lastModified: ''
  13. }
  14. }
  15. );
  1. $updateResult = $db->inventory->replaceOne(
  2. ['item' => 'paper'],
  3. [
  4. 'item' => 'paper',
  5. 'instock' => [
  6. ['warehouse' => 'A', 'qty' => 60],
  7. ['warehouse' => 'B', 'qty' => 40],
  8. ],
  9. ]
  10. );
  1. await db.inventory.replace_one(
  2. {"item": "paper"},
  3. {"item": "paper",
  4. "instock": [
  5. {"warehouse": "A", "qty": 60},
  6. {"warehouse": "B", "qty": 40}]})
  1. Publisher<UpdateResult> replaceOnePublisher = collection.replaceOne(eq("item", "paper"),
  2. Document.parse("{ item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] }"));
  1. var filter = Builders<BsonDocument>.Filter.Eq("item", "paper");
  2. var replacement = new BsonDocument
  3. {
  4. { "item", "paper" },
  5. { "instock", new BsonArray
  6. {
  7. new BsonDocument { { "warehouse", "A" }, { "qty", 60 } },
  8. new BsonDocument { { "warehouse", "B" }, { "qty", 40 } } }
  9. }
  10. };
  11. var result = collection.ReplaceOne(filter, replacement);
  1. $db->coll("inventory")->replace_one(
  2. { item => "paper" },
  3. {
  4. item => "paper",
  5. instock => [ { warehouse => "A", qty => 60 }, { warehouse => "B", qty => 40 } ]
  6. }
  7. );
  1. client[:inventory].replace_one({ item: 'paper' },
  2. { item: 'paper',
  3. instock: [ { warehouse: 'A', qty: 60 },
  4. { warehouse: 'B', qty: 40 } ] })
  1. collection.replaceOne(equal("item", "paper"),
  2. Document("""{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }""")
  3. ).execute()
  1. result, err := coll.ReplaceOne(
  2. context.Background(),
  3. bson.D{
  4. {"item", "paper"},
  5. },
  6. bson.D{
  7. {"item", "paper"},
  8. {"instock", bson.A{
  9. bson.D{
  10. {"warehouse", "A"},
  11. {"qty", 60},
  12. },
  13. bson.D{
  14. {"warehouse", "B"},
  15. {"qty", 40},
  16. },
  17. }},
  18. },
  19. )

Behavior

Atomicity

All write operations in MongoDB are atomic on the level of a singledocument. For more information on MongoDB and atomicity, seeAtomicity and Transactions.

_id Field

Once set, you cannot update the value of the _id field nor can youreplace an existing document with a replacement document that has adifferent _id field value.

Field Order

MongoDB preserves the order of the document fields following writeoperations except for the following cases:

  • The _id field is always the first field in the document.
  • Updates that include renaming of field names mayresult in the reordering of fields in the document.

Changed in version 2.6: Starting in version 2.6, MongoDB actively attempts to preserve thefield order in a document. Before version 2.6, MongoDB did notactively preserve the order of the fields in a document.

  • Mongo Shell
  • Python
  • Java (Sync)
  • Node.js
  • PHP
  • Other
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

Upsert Option

If updateOne(),updateMany(), orreplaceOne() includes upsert : true andno documents match the specified filter, then the operation creates anew document and inserts it. If there are matching documents, then theoperation modifies or replaces the matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If update_one(),update_many(), orreplace_one() includesupsert : true and no documents match the specifiedfilter, then the operation creates a new document and insertsit. If there are matching documents, then the operationmodifies or replaces the matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If the update and replace methods include thecom.mongodb.client.model.UpdateOptions parameter thatspecifies com.mongodb.client.model.UpdateOptions.upsert(true)and no documents match the specified filter, then theoperation creates a new document and inserts it. If there arematching documents, then the operation modifies or replacesthe matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If updateOne(),updateMany(), orreplaceOne() includeupsert : true in the options parameter document andno documents match the specified filter, then the operationcreates a new document and inserts it. If there are matchingdocuments, then the operation modifies or replaces the matchingdocument or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If updateOne(),updateMany(),or replaceOne()includes upsert => true and no documents match thespecified filter, then the operation creates a new document andinserts it. If there are matching documents, then the operationmodifies or replaces the matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If update_one(),update_many(), orreplace_one() includesupsert : true and no documents match the specifiedfilter, then the operation creates a new document and insertsit. If there are matching documents, then the operationmodifies or replaces the matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If the update and replace methods include theUpdateOptionsparameter that specifies UpdateOptions.upsert(true)and no documents match the specified filter, then theoperation creates a new document and inserts it. If there arematching documents, then the operation modifies or replacesthe matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If UpdateOne(),UpdateMany(), orReplaceOne()includes an UpdateOptionsargument instance with the IsUpsert option set to trueand no documents match the specified filter, then theoperation creates a new document and inserts it. If there arematching documents, then the operation modifies or replaces thematching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If update_one(),update_many(), orreplace_one() includesupsert => true and no documents match the specifiedfilter, then the operation creates a new document and insertsit. If there are matching documents, then the operationmodifies or replaces the matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If update_one(),update_many(), orreplace_one() includesupsert => true and no documents match the specifiedfilter, then the operation creates a new document and insertsit. If there are matching documents, then the operationmodifies or replaces the matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If updateOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]), updateMany():org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]), or replaceOne():org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult])includes upsert => true and no documents match the specifiedfilter, then the operation creates a new document and inserts it. Ifthere are matching documents, then the operation modifies or replacesthe matching document or documents.

For details on the new document created, see the individual referencepages for the methods.

Upsert Option

If Collection.UpdateOneincludes the Upsert option set to trueand no documents match thespecified filter, then the operation creates a new documentand inserts it. If there are matching documents, then theoperation modifies or replaces the matching document ordocuments.

For details on the new document created, see the individual referencepages for the methods.

Write Acknowledgement

With write concerns, you can specify the level of acknowledgementrequested from MongoDB for write operations. For details, seeWrite Concern.

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • PHP
    • Motor
    • Java (Async)
    • C#
    • Perl
    • Ruby
    • Scala
    • Go

See also

See also

See also

See also

See also

See also

See also

See also

See also

See also

See also

See also

See also