Aracılığıyla paylaş


findAndModify

komutu findAndModify , tek bir belgeyi atomik olarak değiştirmek ve döndürmek için kullanılır. Bu komut, bir belgenin tek adımda okunmasını ve güncelleştirilmesini gerektiren ve veri tutarlılığını sağlayan işlemler için kullanışlıdır. Yaygın kullanım örnekleri arasında sayaçlar, kuyruklar ve diğer atomik işlemlerin uygulanması yer alır.

Sözdizimi

Komutun findAndModify söz dizimi aşağıdaki gibidir:

db.collection.findAndModify({
   query: <document>,
   sort: <document>,
   remove: <boolean>,
   update: <document>,
   new: <boolean>,
   fields: <document>,
   upsert: <boolean>
})

Parametreler

  • query: Belgenin değiştirileceği seçim ölçütü.
  • sort: Sorgu birden çok belge seçerse hangi belgenin değiştirileceğini belirler.
  • remove: ise true, seçili belgeyi kaldırır.
  • update: Uygulanacak değişiklikler.
  • new: ise true, özgün belge yerine değiştirilmiş belgeyi döndürür.
  • fields: Eşleşen belge için döndürülecek alanları sınırlar.
  • upsert: ise true, sorguyla eşleşen bir belge yoksa yeni bir belge oluşturur.

Örnekler

Örnek 1: Toplam satışları güncelleştirme

Mağazanın toplam satışını "e5767a9f-cd95-439c-9ec4-7ddc13d22926" ile _id güncelleştirmek ve güncelleştirilmiş belgeyi döndürmek 550000.00 istediğimizi varsayalım.

db.stores.findAndModify({
   query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
   update: { $set: { "sales.totalSales": 550000.00 } },
   new: true
})

Örnek çıkış verisi

[mongos] StoreData> db.stores.findAndModify({
...    query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
...    update: { $set: { "sales.totalSales": 550000.00 } },
...    new: true
... })
{
  _id: 'e5767a9f-cd95-439c-9ec4-7ddc13d22926',
  name: "Marina's Eyewear Bargains",
  location: { lat: -87.4376, lon: 42.2928 },
  staff: { totalStaff: { fullTime: 20, partTime: 6 } },
  sales: {
    totalSales: 550000,
    salesByCategory: [
      { categoryName: 'Round Sunglasses', totalSales: 39621 },
      { categoryName: 'Reading Glasses', totalSales: 1146 },
      { categoryName: 'Aviators', totalSales: 9385 }
    ]
  },
  promotionEvents: [
    {
      eventName: 'Incredible Discount Days',
      promotionalDates: {
        startDate: { Year: 2024, Month: 2, Day: 11 },
        endDate: { Year: 2024, Month: 2, Day: 18 }
      },
      discounts: [
        { categoryName: 'Square Sunglasses', discountPercentage: 16 },
        { categoryName: 'Safety Glasses', discountPercentage: 17 },
        { categoryName: 'Wayfarers', discountPercentage: 7 },
        { categoryName: 'Eyewear Accessories', discountPercentage: 12 }
      ]
    }
],
  tag: [
    '#ShopLocal',
    '#FashionStore',
    '#SeasonalSale',
    '#FreeShipping',
    '#MembershipDeals'
  ]
}

Örnek 2: Yeni tanıtım etkinliği ekleme

"e5767a9f-cd95-439c-9ec4-7ddc13d22926" ile _id_ mağazaya "Electronics Super Saver" adlı yeni bir tanıtım etkinliği ekleyelim ve güncelleştirilmiş belgeyi döndürelim.

db.stores.findAndModify({
   query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
   update: { $push: { "promotionEvents": {
       "eventName": "Electronics Super Saver",
       "promotionalDates": {
         "startDate": "2025-09-31",
         "endDate": "2025-09-31"
       },
       "discounts": [
         {
           "categoryName": "Laptops",
           "discountPercentage": 45
         },
         {
           "categoryName": "Smartphones",
           "discountPercentage": 25
         }
       ]
   }}},
   new: true
})

Örnek çıkış verisi

[mongos] StoreData> db.stores.findAndModify({
...    query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
...    update: { $push: { "promotionEvents": {
...        "eventName": "Electronics Super Saver",
...        "promotionalDates": {
...          "startDate": "2025-09-31",
...          "endDate": "2025-09-31"
...        },
...        "discounts": [
...          {
...            "categoryName": "Laptops",
...            "discountPercentage": 45
...          },
...          {
...            "categoryName": "Smartphones",
...            "discountPercentage": 25
...          }
...        ]
...    }}},
...    new: true
... })

{
  _id: 'e5767a9f-cd95-439c-9ec4-7ddc13d22926',
  name: "Marina's Eyewear Bargains",
  location: { lat: -87.4376, lon: 42.2928 },
  staff: { totalStaff: { fullTime: 20, partTime: 6 } },
  sales: {
    totalSales: 550000,
    salesByCategory: [
      { categoryName: 'Round Sunglasses', totalSales: 39621 },
      { categoryName: 'Reading Glasses', totalSales: 1146 },
      { categoryName: 'Aviators', totalSales: 9385 }
    ]
  },
  promotionEvents: [
    {
      eventName: 'Electronics Super Saver',
      promotionalDates: { startDate: '2025-09-31', endDate: '2025-09-31' },
      discounts: [
        { categoryName: 'Laptops', discountPercentage: 45 },
        { categoryName: 'Smartphones', discountPercentage: 25 }
      ]
    }
  ],
  tag: [
    '#ShopLocal',
    '#FashionStore',
    '#SeasonalSale',
    '#FreeShipping',
    '#MembershipDeals'
  ]
}

Örnek 3: Tanıtım etkinliğini kaldırma

"e5767a9f-cd95-439c-9ec4-7ddc13d22926" ile _id "Electronics Super Saver" tanıtım etkinliğini mağazadan kaldırmak ve özgün belgeyi döndürmek istediğimizi varsayalım.

db.stores.findAndModify({
   query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
   update: { $pull: { "promotionEvents": { "eventName": "Electronics Super Saver" } } },
   new: true
})

Örnek çıkış verisi

[mongos] StoreData> db.stores.findAndModify({
...    query: { "_id_": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
...    update: { $pull: { "promotionEvents": { "eventName": "Electronics Super Saver" } } },
...    new: true
... })
null
[mongos] StoreData> db.stores.findAndModify({
...    query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
...    update: { $pull: { "promotionEvents": { "eventName": "Electronics Super Saver" } } },
...    new: true
... })
{
  _id: 'e5767a9f-cd95-439c-9ec4-7ddc13d22926',
  name: "Marina's Eyewear Bargains",
  location: { lat: -87.4376, lon: 42.2928 },
  staff: { totalStaff: { fullTime: 20, partTime: 6 } },
  sales: {
    totalSales: 550000,
    salesByCategory: [
      { categoryName: 'Round Sunglasses', totalSales: 39621 },
      { categoryName: 'Reading Glasses', totalSales: 1146 },
      { categoryName: 'Aviators', totalSales: 9385 }
    ]
  },
  promotionEvents: [
    {
      eventName: 'Incredible Discount Days',
      promotionalDates: {
        startDate: { Year: 2024, Month: 2, Day: 11 },
        endDate: { Year: 2024, Month: 2, Day: 18 }
      },
      discounts: [
        { categoryName: 'Square Sunglasses', discountPercentage: 16 },
        { categoryName: 'Safety Glasses', discountPercentage: 17 },
        { categoryName: 'Wayfarers', discountPercentage: 7 },
        { categoryName: 'Eyewear Accessories', discountPercentage: 12 }
      ]
    }
  ],
  tag: [
    '#ShopLocal',
    '#FashionStore',
    '#SeasonalSale',
    '#FreeShipping',
    '#MembershipDeals'
  ]
}