إشعار
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تسجيل الدخول أو تغيير الدلائل.
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تغيير الدلائل.
insert يتم استخدام الأمر لإنشاء مستندات جديدة في مجموعة. يمكن إدراج مستند واحد أو مستندات متعددة في خطوة واحدة.
إعراب
بناء الجملة الأساسي لأمر الإدراج هو:
db.collection.insert(
<single document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)
Parameters
| المعلمة | Description |
|---|---|
<single document or array of documents> |
المستند أو صفيف المستندات المراد إدراجها في المجموعة |
writeConcern |
(اختياري) مستند يعبر عن قلق الكتابة. يصف قلق الكتابة مستوى الإقرار المطلوب من الخادم لعملية الكتابة |
ordered |
(اختياري) إذا true، يدرج الخادم المستندات بالترتيب المقدم. إذا ، falseيمكن للخادم إدراج المستندات بأي ترتيب وسيحاول إدراج جميع المستندات بغض النظر عن الأخطاء |
-
<single document or array of documents>: المستند أو صفيف المستندات المراد إدراجها في المجموعة. -
writeConcern:اختياري. مستند يعبر عن قلق الكتابة. يصف قلق الكتابة مستوى الإقرار المطلوب من الخادم لعملية الكتابة. -
ordered:اختياري. إذاtrue، يدرج الخادم المستندات بالترتيب المقدم. إذا ،falseيمكن للخادم إدراج المستندات بأي ترتيب وسيحاول إدراج جميع المستندات بغض النظر عن الأخطاء.
المثال (الأمثلة)
إدراج مستند واحد
يدرج الأمر التالي مستندا واحدا في مجموعة المتاجر في قاعدة بيانات StoreData.
db.stores.insertOne({
"storeId": "12345",
"name": "Boulder Innovations",
"location": {
"lat": 37.7749,
"lon": -122.4194
},
"staff": {
"totalStaff": {
"fullTime": 15,
"partTime": 10
}
},
"sales": {
"totalSales": 500000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 300000.00
},
{
"categoryName": "Smartphones",
"totalSales": 200000.00
}
]
},
"promotionEvents": [
{
"eventName": "Summer Sale",
"promotionalDates": {
"startDate": "2024-06-01",
"endDate": "2024-06-30"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 15
},
{
"categoryName": "Smartphones",
"discountPercentage": 10
}
]
},
{
"eventName": "Holiday Specials",
"promotionalDates": {
"startDate": "2024-12-01",
"endDate": "2024-12-31"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 20
},
{
"categoryName": "Smartphones",
"discountPercentage": 25
}
]
}
]
})
إدراج مستندات متعددة
يقوم الأمر التالي بإدراج صفيف من المستندات في مجموعة المتاجر.
db.stores.insertMany([
{
"storeId": "12346",
"name": "Graphic Design Institute",
"location": {
"lat": 34.0522,
"lon": -118.2437
},
"staff": {
"totalStaff": {
"fullTime": 20,
"partTime": 5
}
},
"sales": {
"totalSales": 750000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 400000.00
},
{
"categoryName": "Smartphones",
"totalSales": 350000.00
}
]
},
"promotionEvents": [
{
"eventName": "Black Friday",
"promotionalDates": {
"startDate": "2024-11-25",
"endDate": "2024-11-30"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 25
},
{
"categoryName": "Smartphones",
"discountPercentage": 30
}
]
}
]
},
{
"storeId": "12347",
"name": "Relecloud",
"location": {
"lat": 40.7128,
"lon": -74.0060
},
"staff": {
"totalStaff": {
"fullTime": 10,
"partTime": 20
}
},
"sales": {
"totalSales": 600000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 350000.00
},
{
"categoryName": "Smartphones",
"totalSales": 250000.00
}
]
},
"promotionEvents": [
{
"eventName": "New Year Sale",
"promotionalDates": {
"startDate": "2024-01-01",
"endDate": "2024-01-07"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 10
},
{
"categoryName": "Smartphones",
"discountPercentage": 15
}
]
}
]
}
])
تحديد قيمة لحقل _id
إذا لم يتم تحديد حقل _id، يقوم الخادم تلقائيا بإنشاء قيمة ObjectId() فريدة للمستند. إذا كان المستند يحدد الحقل _id، فيجب أن يكون قيمة فريدة عالميا عبر جميع المستندات داخل المجموعة.
إذا تم تحديد قيمة مكررة لحقل _id، طرح خطأ انتهاك مفتاح مكرر بواسطة الخادم.
{
"WriteErrors": [
{
"WriteError": {
"err": {
"index": 0,
"code": 11000,
"errmsg": "Duplicate key violation on the requested collection: Index '_id_'",
"errInfo": "undefined",
"op": {
"testField": "testValue",
"_id": "1"
}
}
}
}
]
}
إدراج مستندات متعددة بالترتيب
يمكن إدراج المستندات التي يتم إدراجها بشكل مجمع بالترتيب عند تحديد "مرتب": صحيح
db.stores.insertMany([
{
"_id": "123456",
"storeId": "123456",
"name": "Graphic Design Institute",
"location": {
"lat": 34.0522,
"lon": -118.2437
},
"staff": {
"totalStaff": {
"fullTime": 20,
"partTime": 5
}
},
"sales": {
"totalSales": 750000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 400000.00
},
{
"categoryName": "Smartphones",
"totalSales": 350000.00
}
]
},
"promotionEvents": [
{
"eventName": "Black Friday",
"promotionalDates": {
"startDate": "2024-11-25",
"endDate": "2024-11-30"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 25
},
{
"categoryName": "Smartphones",
"discountPercentage": 30
}
]
}
]
},
{
"_id": "234567",
"storeId": "234567",
"name": "Relecloud",
"location": {
"lat": 40.7128,
"lon": -74.0060
},
"staff": {
"totalStaff": {
"fullTime": 10,
"partTime": 20
}
},
"sales": {
"totalSales": 600000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 350000.00
},
{
"categoryName": "Smartphones",
"totalSales": 250000.00
}
]
},
"promotionEvents": [
{
"eventName": "New Year Sale",
"promotionalDates": {
"startDate": "2024-01-01",
"endDate": "2024-01-07"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 10
},
{
"categoryName": "Smartphones",
"discountPercentage": 15
}
]
}
]
}
], "ordered": true)
يرجع أمر الإدراج الذي تم ترتيبه استجابة تؤكد الترتيب الذي تم إدراج المستندات به:
{
"acknowledged": true,
"insertedIds": {
"0": "123456",
"1": "234567"
}
}