Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Polecenie findAndModify służy do niepodzielnego modyfikowania i zwracania pojedynczego dokumentu. To polecenie jest przydatne w przypadku operacji wymagających odczytywania i aktualizowania dokumentu w jednym kroku, zapewniając spójność danych. Typowe przypadki użycia obejmują implementowanie liczników, kolejek i innych operacji niepodzielnych.
Składnia
Składnia findAndModify polecenia jest następująca:
db.collection.findAndModify({
query: <document>,
sort: <document>,
remove: <boolean>,
update: <document>,
new: <boolean>,
fields: <document>,
upsert: <boolean>
})
Parametry
- zapytanie: kryteria wyboru dokumentu do zmodyfikowania.
- sort: określa, który dokument ma być modyfikowany, jeśli zapytanie wybiera wiele dokumentów.
-
remove: Jeśli
truepolecenie spowoduje usunięcie wybranego dokumentu. - update: Modyfikacje, które mają być stosowane.
-
new: Jeśli
true, zwraca zmodyfikowany dokument, a nie oryginalny. - fields: ogranicza pola, które mają być zwracane dla pasującego dokumentu.
-
upsert: Jeśli
trueprogram tworzy nowy dokument, jeśli żaden dokument nie jest zgodny z zapytaniem.
Przykłady
Przykład 1. Aktualizowanie całkowitej sprzedaży
Załóżmy, że chcemy zaktualizować łączną sprzedaż sklepu za pomocą _id polecenia "e5767a9f-cd95-439c-9ec4-7ddc13d22926" i 550000.00 zwrócić zaktualizowany dokument.
db.stores.findAndModify({
query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
update: { $set: { "sales.totalSales": 550000.00 } },
new: true
})
Przykładowe dane wyjściowe
[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'
]
}
Przykład 2. Dodawanie nowego wydarzenia promocyjnego
Dodajmy nowe wydarzenie promocyjne o nazwie "Electronics Super Saver" do sklepu z _id_ "e5767a9f-cd95-439c-9ec4-7ddc13d2292926" i zwróć zaktualizowany dokument.
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
})
Przykładowe dane wyjściowe
[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'
]
}
Przykład 3. Usuwanie wydarzenia promocyjnego
Załóżmy, że chcemy usunąć wydarzenie promocyjne "Electronics Super Saver" ze sklepu z _id "e5767a9f-cd95-439c-9ec4-7ddc13d2292926" i zwrócić oryginalny dokument.
db.stores.findAndModify({
query: { "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" },
update: { $pull: { "promotionEvents": { "eventName": "Electronics Super Saver" } } },
new: true
})
Przykładowe dane wyjściowe
[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'
]
}
Treści powiązane
- Przejrzyj opcje migrowania z bazy danych MongoDB do usługi Azure DocumentDB
- Przeczytaj więcej na temat zgodności funkcji z bazą danych MongoDB