Hướng dẫn mongodb delete many not equal - mongodb xóa nhiều không bằng


Bạn có thể sử dụng toán tử $ ne (viết tắt của không phải là bằng nhau) trong MongoDB để truy vấn cho các tài liệu trong đó trường không bằng một giá trị nhất định.$ne operator (which stands for “not equal”) in MongoDB to query for documents where a field is not equal to a certain value.

Toán tử này sử dụng cú pháp cơ bản sau:

db.myCollection.find({'team': {$ne : "Mavs"}})

Ví dụ cụ thể này tìm thấy tất cả các tài liệu trong bộ sưu tập có tiêu đề MyCollection trong đó trường nhóm không bằng với Mavs.myCollection where the team field is not equal to “Mavs.”

Bạn cũng có thể sử dụng toán tử $ Nin (viết tắt của không phải trong phạm vi) để truy vấn các tài liệu trong đó trường không bằng bất kỳ giá trị nào trong danh sách.$nin operator (which stands for “not in”) to query for documents where a field is not equal to any value in a list.

Toán tử này sử dụng cú pháp cơ bản sau:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})

Ví dụ cụ thể này tìm thấy tất cả các tài liệu trong bộ sưu tập có tiêu đề MyCollection trong đó trường nhóm không bằng với Mavs.myCollection where the team field is not equal to “Mavs”, “Cavs”, or “Spurs.”

Bạn cũng có thể sử dụng toán tử $ Nin (viết tắt của không phải trong phạm vi) để truy vấn các tài liệu trong đó trường không bằng bất kỳ giá trị nào trong danh sách.teams with the following documents:

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})

Ví dụ cụ thể này tìm thấy tất cả các tài liệu trong bộ sưu tập có tiêu đề MyCollection, nơi lĩnh vực nhóm không bằng với Mavs, Cav Cavs, hay Spurs Spurs.

Các ví dụ sau đây cho thấy cách sử dụng từng phương pháp trong thực tế với các nhóm thu thập & nbsp; với các tài liệu sau:teams collection where the “team” field is note equal to “Mavs”:

db.teams.find({'team': {$ne : "Mavs"}})

Ví dụ 1: Truy vấn không bằng nhau

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 

Mã sau đây cho thấy cách tìm tất cả các tài liệu trong bộ sưu tập các nhóm trong đó trường nhóm nhóm của nhóm được ghi chú bằng với Mavs Mavs:teams collection is returned where the team field is not equal to “Mavs.”

Truy vấn này trả về các tài liệu sau:: The $ne operator is case-sensitive.

Lưu ý rằng mọi tài liệu trong bộ sưu tập các nhóm đều được trả lại trong đó trường nhóm không bằng với Mavs.

Lưu ý: Toán tử $ NE nhạy cảm với trường hợp.teams collection where the team field is not equal to “Mavs”, “Cavs”, or “Spurs”:

db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})

Ví dụ 1: Truy vấn không bằng nhau

{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 

Mã sau đây cho thấy cách tìm tất cả các tài liệu trong bộ sưu tập các nhóm trong đó trường nhóm nhóm của nhóm được ghi chú bằng với Mavs Mavs:teams collection is returned where the team field is not equal to “Mavs”, “Cavs”, or “Spurs.”

Truy vấn này trả về các tài liệu sau:: You can find the complete documentation for the $ne function here.

Lưu ý rằng mọi tài liệu trong bộ sưu tập các nhóm đều được trả lại trong đó trường nhóm không bằng với Mavs.: You can find the complete documentation for the $nin function here.

Lưu ý: Toán tử $ NE nhạy cảm với trường hợp.

Ví dụ 2: “Không phải trong truy vấn”

Mã sau đây cho thấy cách tìm tất cả các tài liệu trong bộ sưu tập của các nhóm trong đó trường nhóm không bằng với Mavs Mavs, Cav Cavs, hoặc Spurs Spurs:
MongoDB: How to Query for “not null” in Specific Field
MongoDB: How to Replace Strings

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
3

Quan trọng

Phương pháp Mongosh

Trang này ghi lại một phương thức

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
4. Đây không phải là tài liệu cho trình điều khiển dành riêng cho ngôn ngữ, chẳng hạn như Node.js.
db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
4
method. This is not the documentation for a language-specific driver, such as Node.js.

Đối với trình điều khiển API MongoDB, hãy tham khảo tài liệu trình điều khiển MongoDB dành riêng cho ngôn ngữ.MongoDB driver documentation.

Xóa tất cả các tài liệu phù hợp với

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
5 khỏi một bộ sưu tập.

db.collection.deleteMany(
,
{
writeConcern: <document>,
collation: <document>
}
)

Tham số

Loại hình

Sự mô tả

lọc

tài liệu

Chỉ định tiêu chí xóa bằng cách sử dụng các toán tử truy vấn.

Để xóa tất cả các tài liệu trong một bộ sưu tập, hãy chuyển trong một tài liệu trống (

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
6).

Writeconcern

tài liệu

Chỉ định tiêu chí xóa bằng cách sử dụng các toán tử truy vấn.

Để xóa tất cả các tài liệu trong một bộ sưu tập, hãy chuyển trong một tài liệu trống (

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
6).

Writeconcern

tài liệu

Optional.

Chỉ định tiêu chí xóa bằng cách sử dụng các toán tử truy vấn.

Đối chiếu cho phép người dùng chỉ định các quy tắc cụ thể về ngôn ngữ để so sánh chuỗi, chẳng hạn như các quy tắc cho Lettercase và Dấu nhấn.

Để xóa tất cả các tài liệu trong một bộ sưu tập, hãy chuyển trong một tài liệu trống (

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
6).

collation: {
locale: ,
caseLevel: ,
caseFirst: ,
strength: ,
numericOrdering: ,
alternate: ,
maxVariable: ,
backwards:
}

Writeconcern

Không bắt buộc. Một tài liệu thể hiện mối quan tâm viết. Bỏ qua để sử dụng mối quan tâm ghi mặc định.

Không đặt rõ ràng mối quan tâm ghi cho hoạt động nếu chạy trong một giao dịch. Để sử dụng viết mối quan tâm với các giao dịch, xem giao dịch và viết mối quan tâm.

đối chiếu

Chỉ định đối chiếu để sử dụng cho hoạt động.

tài liệu

Chỉ định tiêu chí xóa bằng cách sử dụng các toán tử truy vấn.query predicate.

Để xóa tất cả các tài liệu trong một bộ sưu tập, hãy chuyển trong một tài liệu trống (

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
6).

Nếu bạn chỉ định một chỉ mục không tồn tại, các lỗi hoạt động.

Lệnh xóa trả về sau:Specify

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
9 for Delete Operations.

Mới trong phiên bản 4.4.

Returns:Trong
db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
4, tạo bộ sưu tập
db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
7 với các tài liệu sau:
  • Tạo các chỉ mục sau trên bộ sưu tập:
  • db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
    9

Hoạt động xóa sau đây gợi ý rõ ràng để sử dụng chỉ mục

db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
8: throws a
db.teams.find({'team': {$ne : "Mavs"}})
5 exception if used on a capped collection. To remove all documents from a capped collection, use
db.teams.find({'team': {$ne : "Mavs"}})
6

Ghi chú throws a

db.teams.find({'team': {$ne : "Mavs"}})
5 exception if used on a time series collection. To remove all documents from a time series collection, use
db.teams.find({'team': {$ne : "Mavs"}})
6

Nếu bạn chỉ định một chỉ mục không tồn tại, các lỗi hoạt động.

Ngoài ra, sử dụng một trường là một phần của một chỉ mục duy nhất như

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
1.

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
3 có thể được sử dụng bên trong các giao dịch đa tài liệu. can be used inside multi-document transactions.

Không đặt rõ ràng mối quan tâm ghi cho hoạt động nếu chạy trong một giao dịch. Để sử dụng viết mối quan tâm với các giao dịch, xem giao dịch và viết mối quan tâm.

Quan trọng

Trong hầu hết các trường hợp, giao dịch đa tài liệu phát sinh chi phí hiệu suất lớn hơn so với ghi tài liệu đơn lẻ và sự sẵn có của các giao dịch đa tài liệu không nên là một sự thay thế cho thiết kế lược đồ hiệu quả. Đối với nhiều kịch bản, mô hình dữ liệu được chuẩn hóa (tài liệu và mảng nhúng) sẽ tiếp tục tối ưu cho các trường hợp dữ liệu và sử dụng của bạn. Đó là, đối với nhiều kịch bản, mô hình hóa dữ liệu của bạn một cách thích hợp sẽ giảm thiểu nhu cầu cho các giao dịch đa tài liệu.

Để biết các cân nhắc sử dụng giao dịch bổ sung (như giới hạn thời gian chạy và giới hạn kích thước oplog), xem thêm các cân nhắc sản xuất.

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
3 xóa tài liệu cùng một lúc. Nếu nút chính không thành công trong quá trình hoạt động
db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
3, các tài liệu chưa bị xóa khỏi các nút thứ cấp không bị xóa khỏi bộ sưu tập. deletes documents one at a time. If the primary node fails during a
db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
3
operation, documents that were not yet deleted from secondary nodes are not deleted from the collection.

Bộ sưu tập

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
5 có các tài liệu với cấu trúc sau:

{
_id: ObjectId("563237a41a4d68582c2509da"),
stock: "Brent Crude Futures",
qty: 250,
type: "buy-limit",
limit: 48.90,
creationts: ISODate("2015-11-01T12:30:15Z"),
expiryts: ISODate("2015-11-01T12:35:15Z"),
client: "Crude Traders Inc."
}

Hoạt động sau đây xóa tất cả các tài liệu trong đó

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
6:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
0

Hoạt động trả về:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
1

Hoạt động sau đây xóa tất cả các tài liệu trong đó

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
7 và
{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
8 lớn hơn
{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
9:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
2

Hoạt động trả về:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
3

Hoạt động sau đây xóa tất cả các tài liệu trong đó

{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
7 và
{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
8 lớn hơn
{ _id: ObjectId("6203ec0e1e95a9885e1e7658"),
  team: 'Cavs',
  points: 23,
  rebounds: 9 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7656"),
  team: 'Rockets',
  points: 20,
  rebounds: 7 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7655"),
  team: 'Spurs',
  points: 35,
  rebounds: 12 }
{ _id: ObjectId("6203ec0e1e95a9885e1e7657"),
  team: 'Warriors',
  points: 25,
  rebounds: 5 } 
9:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
4

Đưa ra một bộ bản sao ba thành viên, thao tác sau đây chỉ định

db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
0 của
db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
1 và
db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
2 của
db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
3:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
5

Nếu sự thừa nhận mất nhiều thời gian hơn giới hạn db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})2, thì ngoại lệ sau được ném:

Mẹo

Xem thêm:

Đối chiếu cho phép người dùng chỉ định các quy tắc cụ thể về ngôn ngữ để so sánh chuỗi, chẳng hạn như các quy tắc cho Lettercase và Dấu nhấn.

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
6

Bộ sưu tập

db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
5 có các tài liệu sau:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
7

Hoạt động sau bao gồm tùy chọn đối chiếu:

Mới trong phiên bản 4.4.

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
4, create a
db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
7 collection with the following documents:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
8

Trong

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
4, tạo bộ sưu tập
db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
7 với các tài liệu sau:

Tạo các chỉ mục sau trên bộ sưu tập:

db.myCollection.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
9

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
0

Hoạt động xóa sau đây gợi ý rõ ràng để sử dụng chỉ mục db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})8:

Ghi chú

Nếu bạn chỉ định một chỉ mục không tồn tại, các lỗi hoạt động.

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
1

Lệnh xóa trả về sau:

db.teams.insertOne({team: "Mavs", points: 30, rebounds: 8})
db.teams.insertOne({team: "Spurs", points: 35, rebounds: 12})
db.teams.insertOne({team: "Rockets", points: 20, rebounds: 7})
db.teams.insertOne({team: "Warriors", points: 25, rebounds: 5})
db.teams.insertOne({team: "Cavs", points: 23, rebounds: 9})
2

Để xem các chỉ mục được sử dụng, bạn có thể sử dụng đường ống

db.teams.find({'team': {$nin : ["Mavs", "Cavs", "Spurs"]}})
9:

Làm cách nào để xóa nhiều dữ liệu trong MongoDB?

Shell MongoDB cung cấp các phương pháp sau để xóa tài liệu khỏi bộ sưu tập:..
Để xóa nhiều tài liệu, sử dụng DB. thu thập. Deletemany ().
Để xóa một tài liệu duy nhất, sử dụng DB. thu thập. Xóa ().

Không bằng trong MongoDB?

MongoDB cung cấp các loại vận hành so sánh và bất bình đẳng khác nhau hoặc không bằng toán tử ($ ne) là một trong số đó. Toán tử này được sử dụng để chọn các tài liệu trong đó giá trị của trường không bằng giá trị đã cho. Nó cũng bao gồm những tài liệu không chứa trường được chỉ định.This operator is used to select those documents where the value of the field does not equal to the given value. It also includes those documents that do not contain the specified field.

Làm thế nào để bạn viết một truy vấn không bằng nhau trong MongoDB?

Bạn có thể sử dụng toán tử $ ne (viết tắt của không phải là bằng nhau) trong MongoDB để truy vấn cho các tài liệu trong đó trường không bằng một giá trị nhất định.Ví dụ cụ thể này tìm thấy tất cả các tài liệu trong bộ sưu tập có tiêu đề MyCollection trong đó trường nhóm không bằng với Mavs.$ne operator (which stands for “not equal”) in MongoDB to query for documents where a field is not equal to a certain value. This particular example finds all documents in the collection titled myCollection where the team field is not equal to “Mavs.”

$ Nin trong MongoDB là gì?

$ Nin chọn các tài liệu trong đó: Giá trị trường không nằm trong mảng được chỉ định hoặc.Trường không tồn tại.selects the documents where: the field value is not in the specified array or. the field does not exist.