Vì vậy, tôi đang sử dụng khung tổng hợp để tổng hợp một số kết quả. Câu hỏi của tôi là làm cách nào để tránh trả lại
// MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
3 mỗi lần nếu có kết quả tổng bằng 0Tôi chỉ muốn hiển thị
// MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
4 và loại bỏ // MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
3 khácliên kết
- stackoverflow. comghi bàn. 1
câu trả lời được chấp nhận
Những gì bạn đang cố gắng làm là xóa một trường khỏi tài liệu trong đường dẫn dựa trên giá trị của trường. Tôi không tin rằng điều này có thể thực hiện được với khung tổng hợp [kể từ 2. 6]. Tuy nhiên, có vẻ như đó không phải là vấn đề, vì việc bỏ qua kết quả
// MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
6 ở bất cứ nơi nào bạn đang sử dụng chúng là chuyện nhỏ. Sẽ rất hữu ích khi xem một quy trình tổng hợp đầy đủ, bởi vì có thể có những sửa đổi trước đó trong quy trình sẽ ngăn chặn bất kỳ trường // MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
7 nào được tạo với giá trị 0, có thể nói là giải quyết vấn đề theo hướng khác Với
// MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
8, tôi nhận được chỉ mục là "johndoe". Vấn đề là. Tôi không thể sử dụng // MongoDB version 5.0.8
> db.collection.insertMany[[
{
contact: {
phone: [
{"type" : "UAN", "number" : "123456789"},
{"type" : "office", "number" : "987546321"},
{"type" : "personal", "number" : "852147963"}
]
},
services: ["Google", "Instagram", "Twitter", "Facebook"],
scores: [ 0, 1, 8, 17, 18, 8, 20, 10 ]
},
{
contact: {
phone: [
{"type" : "UAN", "number" : "456321879"},
{"type" : "office", "number" : "874596321"},
{"type" : "personal", "number" : "964785123"}
]
},
services: ["Google", "Instagram", "Twitter"],
scores: [ 1, 8, 2, 3, 8, 7, 1 ]
}
]];
9 vì điều này cần 2 hoặc ba đối số làm điểm bắt đầu hoặc làm điểm bắt đầu + số để giữ. Nhưng lấy mảng này["johndoe", "janedoe", "jamesdoe"]
Bằng cách nào, với một đường dẫn tổng hợp, tôi có thể chuyển đổi mảng này thành **remove "janedoe" [chỉ mục. 1]`` từ mảng, vì vậy sau đường dẫn tổng hợp, tôi muốn cái này
Chúng tôi có thể áp dụng các cách tiếp cận khác nhau tùy thuộc vào yêu cầu của dự án. Hôm nay, chúng ta sẽ học cách sử dụng
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7, // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
8, // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
9, {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 và toán tử vị trí [{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1] để xóa phần tử khỏi mảng hoặc mảng tài liệu khi làm việc trong MongoDBChúng ta phải có một bộ sưu tập [các] tài liệu mẫu để thực hành tất cả các cách đã đề cập. Chúng tôi sẽ tạo một bộ sưu tập có tên là
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2 và chỉ chèn một tài liệuBạn cũng có thể sử dụng
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2 sau có chứa một tài liệuMã ví dụ
________số 8Nếu bạn không có bộ sưu tập có tên
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2, đừng lo lắng; . Tiếp theo, bạn có thể chạy {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
5 để xem tài liệu đã chènHãy nhớ rằng, bạn sẽ không thấy các tài liệu được cập nhật sau khi thực hiện các ví dụ mã sắp tới. Để làm được điều đó, bạn phải thực hiện lệnh
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
5Sử dụng MongoDB // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7 để xóa phần tử khỏi một mảng tài liệu
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
Mã ví dụ
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
ĐẦU RA
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
Ở đây, chúng tôi đang sử dụng toán tử
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7 để xóa tất cả các phiên bản của một giá trị/giá trị khỏi một mảng hiện có khớp với truy vấn bộ lọc hoặc điều kiện đã chỉ địnhTrong ví dụ mã trên, chúng tôi đang tìm kiếm một tài liệu trong đó
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
9 bằng với // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
70. Sau khi tài liệu khớp với điều kiện đã chỉ định, chúng tôi xóa đối tượng khỏi một mảng hiện có trong đó // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
71 bằng với // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
72Hãy nhớ những điểm sau khi sử dụng toán tử
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7- Nếu
74 đã cho cần xóa là một mảng, thì toán tử// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
7 chỉ xóa các phần tử khỏi một mảng thỏa mãn chính xác// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
74 đã cho [bao gồm cả thứ tự]// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
- Nếu
74 đã cho cần xóa là một tài liệu, thì toán tử// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
7 chỉ xóa các phần tử khỏi một mảng chứa các trường & giá trị chính xác. Thứ tự của các trường có thể khác nhau ở đây// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
- Nếu
79 đã cho và các phần tử của mảng là các tài liệu được nhúng, thì toán tử// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
7 sẽ áp dụng// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
79 như thể mọi phần tử mảng là một tài liệu trong một bộ sưu tập. Để biết thêm chi tiết về điều đó, bạn có thể kiểm tra điều này// MongoDB version 5.0.8 > db.collection.update[ { _id : ObjectId["62aae796f27219958a489b89"] }, { $pull : { "contact.phone": { "number": "987546321" } } } ];
Bây giờ, chúng tôi có một tình huống khác mà chúng tôi muốn thực hiện thao tác
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
92 trên tất cả các tài liệu trong bộ sưu tập. Để làm điều đó, chúng tôi sử dụng cú pháp sau sử dụng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7 trong khi // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
94 hoạt động trên tất cả các tài liệu của điều kiện đã chỉ địnhMã ví dụ
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7ĐẦU RA
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
9Để có được đầu ra ở trên, chúng tôi xóa phần tử mảng khỏi tất cả các tài liệu trong bộ sưu tập trong đó
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
95 bằng với // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
96Nhiệm vụ tiếp theo của chúng ta là xóa các phần tử khỏi mảng
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
97 có giá trị lớn hơn hoặc bằng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
98 và nhỏ hơn hoặc bằng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
99 [ // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
90 ]. Thực hiện truy vấn sau để thực hiện điều đó cho tất cả các tài liệu của bộ sưu tập đã chọnMã ví dụ
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
9ĐẦU RA
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1Để thực hành với các giá trị kiểu chuỗi, chúng ta có thể làm theo cách sau
Mã ví dụ
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
2ĐẦU RA
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
3Đối với ví dụ mã này, chúng tôi sử dụng toán tử so sánh
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
91 để có một mảng các giá trị và loại bỏ chúng khỏi mảng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
92 nếu có bất kỳ giá trị nàoHơn nữa, chúng tôi xóa
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
93 khỏi mảng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
97 nếu nó ở đó trong khi chúng tôi đang sử dụng toán tử // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7 để thực hiện tất cả thao tác xóa nàySử dụng MongoDB // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
8 để xóa phần tử khỏi một mảng
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
Mã ví dụ
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0ĐẦU RA
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
0Đối với mã này, chúng tôi đang sử dụng toán tử
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
8 để xóa tất cả các lần xuất hiện [trường hợp] của giá trị đã cho khỏi một mảng đã tồn tại. Chẳng hạn, nếu số // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
98 xuất hiện hai lần trong mảng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
97, thì cả hai giá trị // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
98 sẽ bị xóaGiống như toán tử
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
7 loại bỏ các phần tử mảng bằng cách chỉ định truy vấn bộ lọc, // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
8 xóa/loại bỏ các phần tử mảng khớp với các giá trị được liệt kêChúng tôi sử dụng ký hiệu dấu chấm để xử lý các tài liệu nhúng của mảng
Sử dụng MongoDB // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
9 để xóa phần tử khỏi một mảng
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
Chúng ta có thể sử dụng toán tử
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
9 nếu muốn loại bỏ phần tử đầu tiên hoặc cuối cùng khỏi một mảng. Đối với điều này, chúng tôi chuyển {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
15 hoặc // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
93 cho toán tử // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
9 để xóa một phần tử mảng khỏi đầu tiên hoặc cuối cùngMã ví dụ
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
1ĐẦU RA
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
2Sử dụng MongoDB {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 để xóa trường chứa phần tử đã cho
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
Mã ví dụ
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
3ĐẦU RA
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
4Ở đây, chúng tôi sử dụng toán tử
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 để xóa một trường cụ thể chứa giá trị đã cho. Chẳng hạn, chúng tôi đang xóa trường // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
97 khớp với truy vấn bộ lọc và chứa // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
98 dưới dạng phần tử mảngHãy nhớ rằng khi chúng ta sử dụng toán tử
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 với toán tử vị trí [{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1], nó không loại bỏ phần tử phù hợp mà đặt nó thành {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
24. Xem phần sau để thực hành điều đóSử dụng Toán tử vị trí MongoDB để đặt phần tử thành {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
24 thay vì xóa
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
Mã ví dụ
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
5ĐẦU RA
// MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
6Đoạn mã này sử dụng
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
0 với toán tử vị trí để đặt phần tử của mảng thành {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
24 thay vì loại bỏ nó. Chúng tôi sử dụng {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
1 để cập nhật phần tử đầu tiên của mảng có giá trị là {
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
29 trong mảng // MongoDB version 5.0.8
> db.collection.update[
{ _id : ObjectId["62aae796f27219958a489b89"] },
{ $pull : { "contact.phone": { "number": "987546321" } } }
];
92Cách làm này có thể sử dụng khi chúng ta không biết vị trí của một phần tử trong mảng. Mặt khác, chúng ta cũng có thể sử dụng
{
"_id" : ObjectId["62aae796f27219958a489b89"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "123456789" },
{ "type" : "personal", "number" : "852147963" }
]
},
"services" : [ "Google", "Instagram", "Twitter", "Facebook" ],
"scores" : [ 0, 1, 8, 17, 18, 8, 20, 10 ]
}
{
"_id" : ObjectId["62aae796f27219958a489b8a"],
"contact" : {
"phone" : [
{ "type" : "UAN", "number" : "456321879" },
{ "type" : "office", "number" : "874596321" },
{ "type" : "personal", "number" : "964785123" }
]
},
"services" : [ "Google", "Instagram", "Twitter" ],
"scores" : [ 1, 8, 2, 3, 8, 7, 1 ]
}
31 [tất cả các toán tử vị trí] nếu chúng ta muốn cập nhật tất cả các phần tử trong trường mảng đã cho