Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Quan trọng
Phương pháp Mongosh
Trang này ghi lại một phương thức
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Đố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.
Đối với tài liệu Shell Legacy
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Mongo Shell v4.4
Mongo Shell v4.2
Chọn các tài liệu trong một bộ sưu tập hoặc xem và trả về con trỏ cho các tài liệu đã chọn.
truy vấn | tài liệu | Không bắt buộc. Chỉ định bộ lọc lựa chọn bằng toán tử truy vấn. Để trả về tất cả các tài liệu trong một bộ sưu tập, hãy bỏ qua tham số này hoặc chuyển một tài liệu trống (
| |||||||||||
chiếu | tài liệu | Không bắt buộc. Chỉ định bộ lọc lựa chọn bằng toán tử truy vấn. Để trả về tất cả các tài liệu trong một bộ sưu tập, hãy bỏ qua tham số này hoặc chuyển một tài liệu trống (
| |||||||||||
chiếu | tài liệu | Không bắt buộc. Chỉ định bộ lọc lựa chọn bằng toán tử truy vấn. Để trả về tất cả các tài liệu trong một bộ sưu tập, hãy bỏ qua tham số này hoặc chuyển một tài liệu trống (
|
chiếu
|
Quan trọng
Không bắt buộc. Chỉ định các trường để trả về trong các tài liệu phù hợp với bộ lọc truy vấn. Để trả về tất cả các trường trong các tài liệu phù hợp, hãy bỏ qua tham số này. Để biết chi tiết, xem phép chiếu.
tùy chọn
{ <field1>: <value>, <field2>: <value> ... }
| truy vấn | ||||||||||||||||||||||||||||||||||||||||||||
db.bios.find( { _id: 5 } ) | tài liệu | ||||||||||||||||||||||||||||||||||||||||||||
db.bios.find( { _id: 5 } ) | Không bắt buộc. Chỉ định bộ lọc lựa chọn bằng toán tử truy vấn. Để trả về tất cả các tài liệu trong một bộ sưu tập, hãy bỏ qua tham số này hoặc chuyển một tài liệu trống ( db.bios.find( { _id: 5 } ) db.bios.find( { _id: 5 } ) | ||||||||||||||||||||||||||||||||||||||||||||
db.bios.find( { _id: 5 } ) | chiếu | ||||||||||||||||||||||||||||||||||||||||||||
db.bios.find( { _id: 5 } ) | Không bắt buộc. Chỉ định các trường để trả về trong các tài liệu phù hợp với bộ lọc truy vấn. Để trả về tất cả các trường trong các tài liệu phù hợp, hãy bỏ qua tham số này. Để biết chi tiết, xem phép chiếu. | ||||||||||||||||||||||||||||||||||||||||||||
db.bios.find( { "name.last": "Hopper" } ) | tùy chọn Không bắt buộc. Chỉ định các tùy chọn bổ sung cho truy vấn. Các tùy chọn này sửa đổi hành vi truy vấn và cách kết quả được trả về. Để xem các tùy chọn có sẵn, xem FindOptions.
Tham số
Chiếu |
Chỉ định bao gồm một trường. Số nguyên khác không cũng được coi là
db.bios.find( { _id: 5 } )
0.Chỉ định loại trừ một trường.
Với việc sử dụng toán tử chiếu mảng
db.bios.find( { _id: 5 } )
3, bạn có thể chỉ định trình chiếu để trả về phần tử đầu tiên phù hợp với điều kiện truy vấn trên trường mảng; ví dụ.db.bios.find( { _id: 5 } )
4. (Không có sẵn để xem.) Số nguyên khác nhau cũng được coi làdb.bios.find( { _id: 5 } )
0.
Sử dụng các toán tử chiếu mảng
db.bios.find( { _id: 5 } )
7,db.bios.find( { _id: 5 } )
8, chỉ định (các) phần tử mảng để bao gồm, do đó loại trừ các phần tử không đáp ứng các biểu thức. (Không có sẵn để xem.)Sử dụng biểu thức toán tử
db.bios.find( { "name.last": "Hopper" } )
0, chỉ định bao gồmdb.bios.find( { "name.last": "Hopper" } )
1 có sẵn. (Không có sẵn để xem.)Chỉ định giá trị của trường dự kiến.
Bắt đầu từ MongoDB 4.4, với việc sử dụng các biểu thức tổng hợp và cú pháp, bao gồm việc sử dụng các biến số và biến tổng hợp, bạn có thể chiếu các trường mới hoặc chiếu các trường hiện có với các giá trị mới. Ví dụ,
Nếu bạn chỉ định một nghĩa đen không phải là người Boolean (như chuỗi theo nghĩa đen hoặc một mảng hoặc biểu thức toán tử) cho giá trị chiếu, trường được chiếu với giá trị mới; ví dụ.:Projection Examples.
Để chiếu một giá trị theo nghĩa đen cho một trường, hãy sử dụng biểu thức tổng hợp
db.bios.find( { "name.last": "Hopper" } )
6; ví dụ.:db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 in{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
3 to continue iteration.Trong các phiên bản 4.2 và sớm hơn, bất kỳ giá trị đặc tả nào (ngoại trừ giá trị tài liệu chưa được hỗ trợ trước đó) được coi là
db.bios.find( { _id: 5 } )
0 hoặcdb.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
Mới trong phiên bản 4.4.
Đối với các trường trong một tài liệu nhúng, bạn có thể chỉ định trường bằng cách sử dụng: db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
5 method.ký hiệu chấm; ví dụ.
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Truy vấn sau sử dụng
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
6 để trả về các tài liệu trong đó giá trị củadb.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
7 lớn hơndb.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
8.db.collection.find( { qty: { $gt: 4 } } )
Truy vấn trả về các tài liệu sau:
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Tài liệu có
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
7 của nó thuộc loạidb.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
6 thuộc loạidb.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
Tài liệu có
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
7 của nó thuộc loạidb.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
Ghi chú
Mới trong phiên bản 4.0.
Đối với con trỏ được tạo trong một phiên, bạn không thể gọi
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
Tương tự, đối với con trỏ được tạo ra bên ngoài phiên, bạn không thể gọi
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
Bắt đầu từ MongoDB 3.6, Trình điều khiển MongoDB và
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Nếu một phiên nhàn rỗi trong hơn 30 phút, máy chủ MongoDB sẽ đánh dấu phiên đó là hết hạn và có thể đóng nó bất cứ lúc nào. Khi máy chủ MongoDB đóng phiên, nó cũng giết chết mọi hoạt động đang thực hiện và các con trỏ mở liên quan đến phiên. Điều này bao gồm các con trỏ được cấu hình với
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Đối với các hoạt động có thể nhàn rỗi trong hơn 30 phút, liên kết hoạt động với một phiên rõ ràng bằng cách sử dụng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
-
Đối với con trỏ được tạo ra bên ngoài giao dịch, bạn không thể gọi
9 bên trong giao dịch.db.bios.find( { "name.last": { $regex: /^N/ } } ) Đối với con trỏ được tạo trong một giao dịch, bạn không thể gọi
9 ngoài giao dịch.db.bios.find( { "name.last": { $regex: /^N/ } } )
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.
Bắt đầu từ MongoDB 4.2, nếu khách hàng phát hành
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 ngắt kết nối trước khi hoạt động hoàn thành, MongoDB đánh dấudb.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 để chấm dứt bằng cách sử dụng{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 disconnects before the operation completes, MongoDB marksdb.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 for termination using{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Các ví dụ trong phần này sử dụng các tài liệu từ Bộ sưu tập BIOS trong đó các tài liệu thường có biểu mẫu:
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Để tạo và điền vào bộ sưu tập
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Phương thức
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Hoạt động sau trả về các tài liệu trong Bộ sưu tập BIOS trong đó
4 bằngdb.bios.find( { _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } )
18:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.bios.find( { _id: 5 } )
Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó trường
19 trong tài liệu nhúng{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
20 bằng{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
21:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.bios.find( { "name.last": "Hopper" } )
Ghi chú
Mới trong phiên bản 4.0.
Đối với con trỏ được tạo trong một phiên, bạn không thể gọi
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Tương tự, đối với con trỏ được tạo ra bên ngoài phiên, bạn không thể gọi
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
Bắt đầu từ MongoDB 3.6, Trình điều khiển MongoDB và
1 liên kết tất cả các hoạt động với phiên máy chủ, ngoại trừ các hoạt động ghi không được biết đến. Đối với các hoạt động không được liên kết rõ ràng với một phiên (nghĩa là sử dụng ____102), trình điều khiển MongoDB và{ "_id" : <value>, "name" : { "first" : <string>, "last" : <string> }, // embedded document "birth" : <ISODate>, "death" : <ISODate>, "contribs" : [ <string>, ... ], // Array of Strings "awards" : [ { "award" : <string>, year: <number>, by: <string> } // Array of embedded documents ... ] }
1 tạo một phiên ngầm và liên kết nó với hoạt động.{ "_id" : <value>, "name" : { "first" : <string>, "last" : <string> }, // embedded document "birth" : <ISODate>, "death" : <ISODate>, "contribs" : [ <string>, ... ], // Array of Strings "awards" : [ { "award" : <string>, year: <number>, by: <string> } // Array of embedded documents ... ] } db.bios.find( { _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } ) Nếu một phiên nhàn rỗi trong hơn 30 phút, máy chủ MongoDB sẽ đánh dấu phiên đó là hết hạn và có thể đóng nó bất cứ lúc nào. Khi máy chủ MongoDB đóng phiên, nó cũng giết chết mọi hoạt động đang thực hiện và các con trỏ mở liên quan đến phiên. Điều này bao gồm các con trỏ được cấu hình với
04 hoặc{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
05 lớn hơn 30 phút.{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
Đối với các hoạt động có thể nhàn rỗi trong hơn 30 phút, liên kết hoạt động với một phiên rõ ràng bằng cách sử dụng
02 và định kỳ làm mới phiên bằng lệnh{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
07. Xem thời gian chờ phiên nhàn rỗi để biết thêm thông tin.{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.bios.find( { "name.last": { $regex: /^N/ } } )
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 có thể được sử dụng bên trong các giao dịch đa tài liệu.Đối với con trỏ được tạo ra bên ngoài giao dịch, bạn không thể gọi
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 có thể được sử dụng bên trong các giao dịch đa tài liệu.Đối với con trỏ được tạo ra bên ngoài giao dịch, bạn không thể gọi
db.bios.find( |
{ "name.last": { $regex: /^N/ } } |
) |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Để biết danh sách các toán tử truy vấn, xem Trình chọn truy vấn.
Các ví dụ sau đây truy vấn trường nhúng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS trong đó tài liệu nhúng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Trường
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS trong đó tài liệu nhúng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Truy vấn khớp với tài liệu trong đó trường
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Để biết thêm thông tin và ví dụ, xem thêm Truy vấn trên các tài liệu nhúng/lồng nhau.
Các ví dụ sau đây truy vấn mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó trường mảng
60 chứa phần tử{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
62:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
6{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó trường mảng
60 chứa phần tử{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
64 hoặc{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
65:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
7{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } Hoạt động sau sử dụng toán tử truy vấn
66 để trả về các tài liệu trong bộ sưu tập BIOS trong đó trường mảng{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
60 chứa cả hai phần tử{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
64 và{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
65:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
8{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } Để biết thêm ví dụ, xem
66. Xem thêm{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.bios.find( { _id: 5 } )
7Hoạt động sau sử dụng toán tử
72 để trả về các tài liệu trong bộ sưu tập BIOS trong đó kích thước mảng là{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
60 là 4:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
9{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
Để biết thêm thông tin và ví dụ về truy vấn một mảng, xem:
Truy vấn một mảng
Truy vấn một loạt các tài liệu nhúng
Để biết danh sách các toán tử truy vấn cụ thể mảng, xem Array.
Các ví dụ sau đây truy vấn mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
74 chứa một phần tử với trường{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
76 bằng{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
77:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.collection.find( { qty: { $gt: 4 } } )
0Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
74 chứa ít nhất một phần tử với cả trường{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
76 bằng{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
77 và trường{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" }
81 lớn hơn 1980:{ "_id": "apples", "qty": 5 } { "_id": "bananas", "qty": 7 } { "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } { "_id": "avocados", "qty": "fourteen" } db.collection.find( { qty: { $gt: 4 } } )
1Sử dụng toán tử
db.bios.find( { _id: 5 } )
7 để chỉ định nhiều tiêu chí trên một phần tử mảng.
Để biết thêm thông tin và ví dụ về truy vấn một mảng, xem:
Truy vấn một mảng
Truy vấn một loạt các tài liệu nhúng
Để biết danh sách các toán tử truy vấn cụ thể mảng, xem Array.
Các ví dụ sau đây truy vấn mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Sử dụng toán tử
db.bios.find( { _id: 5 } )
7 để chỉ định nhiều tiêu chí trên một phần tử mảng.db.collection.find( { qty: { $gt: 4 } } )
2Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Sử dụng toán tử
db.bios.find( { _id: 5 } )
7 để chỉ định nhiều tiêu chí trên một phần tử mảng.db.collection.find( { qty: { $gt: 4 } } )
3Hoạt động sau trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS trong đó mảng
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Sử dụng toán tử
db.bios.find( { _id: 5 } )
7 để chỉ định nhiều tiêu chí trên một phần tử mảng.db.collection.find( { qty: { $gt: 4 } } )
4Tham số chiếu chỉ định các trường nào sẽ trả về. Tham số chứa bao gồm hoặc loại trừ các thông số kỹ thuật, không phải cả hai, trừ khi loại trừ dành cho trường
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.collection.find( { qty: { $gt: 4 } } )
5Ghi chú
db.collection.find( { qty: { $gt: 4 } } )
6Trừ khi trường
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 projection can accept aggregation expressions and syntax.Hoạt động sau đây tìm thấy tất cả các tài liệu trong Bộ sưu tập BIOS và chỉ trả về trường
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
db.bios.find( |
{ _id: { $in: [ 5, ObjectId("507c35dd8fada716c89d0013") ] } } |
) |
db.collection.find( { qty: { $gt: 4 } } )
7Để đặt trường
db.collection.find( { qty: { $gt: 4 } } )
10 thành giá trịdb.collection.find( { qty: { $gt: 4 } } )
11, thao tác trả về các tài liệu sau:db.collection.find( { qty: { $gt: 4 } } )
8Mẹo
Xem thêm:
Phương pháp
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
Trong
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
db.collection.find( { qty: { $gt: 4 } } )
14, con trỏ sẽ tự động lặp lại để truy cập vào 20 tài liệu đầu tiên khớp với truy vấn. Bạn có thể đặt biếndb.collection.find( { qty: { $gt: 4 } } )
15 để thay đổi số lượng tài liệu lặp tự động.{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
db.collection.find( { qty: { $gt: 4 } } )
14 keyword, the cursor is automatically iterated to access up to the first 20 documents that match the query. You can set thedb.collection.find( { qty: { $gt: 4 } } )
15 variable to change the number of automatically iterated documents.Để lặp theo cách thủ công qua các kết quả, gán con trỏ được trả về cho một biến với từ khóa
db.collection.find( { qty: { $gt: 4 } } )
14, như được hiển thị trong các phần sau.Ví dụ sau sử dụng biến
db.collection.find( { qty: { $gt: 4 } } )
17 để lặp lại trên con trỏ và in các tài liệu phù hợp:db.collection.find( { qty: { $gt: 4 } } )
9Ví dụ sau sử dụng phương pháp con trỏ
db.collection.find( { qty: { $gt: 4 } } )
18 để truy cập các tài liệu:{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Để in, bạn cũng có thể sử dụng phương thức
db.collection.find( { qty: { $gt: 4 } } )
19 thay vìdb.collection.find( { qty: { $gt: 4 } } )
20:{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Ví dụ sau sử dụng phương pháp con trỏ
db.collection.find( { qty: { $gt: 4 } } )
21 để lặp lại con trỏ và truy cập các tài liệu:{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
1 và các trình điều khiển cung cấp một số phương thức con trỏ kêu gọi con trỏ được trả về bằng phương pháp { "_id" : <value>, "name" : { "first" : <string>, "last" : <string> }, // embedded document "birth" : <ISODate>, "death" : <ISODate>, "contribs" : [ <string>, ... ], // Array of Strings "awards" : [ { "award" : <string>, year: <number>, by: <string> } // Array of embedded documents ... ] }
6 để sửa đổi hành vi của nó. and the drivers provide several cursor methods that call on the cursor returned by the
{ "_id" : <value>, "name" : { "first" : <string>, "last" : <string> }, // embedded document "birth" : <ISODate>, "death" : <ISODate>, "contribs" : [ <string>, ... ], // Array of Strings "awards" : [ { "award" : <string>, year: <number>, by: <string> } // Array of embedded documents ... ] }
6 method to modify its behavior.{ "_id" : <value>, "name" : { "first" : <string>, "last" : <string> }, // embedded document "birth" : <ISODate>, "death" : <ISODate>, "contribs" : [ <string>, ... ], // Array of Strings "awards" : [ { "award" : <string>, year: <number>, by: <string> } // Array of embedded documents ... ] }
Phương thức
db.collection.find( { qty: { $gt: 4 } } )
24 đặt hàng các tài liệu trong tập kết quả. Hoạt động sau đây trả về các tài liệu trong bộ sưu tập BIOS được sắp xếp theo thứ tự tăng dần theo trường{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
db.collection.find( { qty: { $gt: 4 } } )
24 tương ứng với câu lệnhdb.collection.find( { qty: { $gt: 4 } } )
27 trong SQL.Phương pháp
db.collection.find( { qty: { $gt: 4 } } )
28 giới hạn số lượng tài liệu trong tập kết quả. Hoạt động sau đây trả về nhiều nhất{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
db.collection.find( { qty: { $gt: 4 } } )
28 tương ứng với câu lệnhdb.collection.find( { qty: { $gt: 4 } } )
31 trong SQL.Phương pháp
db.collection.find( { qty: { $gt: 4 } } )
32 kiểm soát điểm bắt đầu của tập kết quả. Hoạt động sau bỏ qua các tài liệu{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
{ "_id": "oranges", "qty": { "in stock": 8, "ordered": 12 } } |
{ "_id": "avocados", "qty": "fourteen" } |
Đố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.
Phương pháp
db.collection.find( { qty: { $gt: 4 } } )
34 chỉ định đối chiếu cho hoạt độngdb.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1.db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )
1 operation.{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Các báo cáo sau đây chuỗi các phương pháp con trỏ
db.collection.find( { qty: { $gt: 4 } } )
28 vàdb.collection.find( { qty: { $gt: 4 } } )
24{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Hai tuyên bố là tương đương; tức là thứ tự mà bạn chuỗi các phương pháp
db.collection.find( { qty: { $gt: 4 } } )
28 vàdb.collection.find( { qty: { $gt: 4 } } )
24 không đáng kể. Cả hai câu lệnh trả về năm tài liệu đầu tiên, như được xác định bởi thứ tự sắp xếp tăng dần trên 'tên'.
|
|
Bạn có thể chỉ định các tùy chọn truy vấn để sửa đổi hành vi truy vấn và cho biết cách trả về kết quả.
Ví dụ: để xác định các biến mà bạn có thể truy cập ở nơi khác trong phương thức
{ |
"_id" : <value>, |
"name" : { "first" : <string>, "last" : <string> }, // embedded document |
"birth" : <ISODate>, |
"death" : <ISODate>, |
"contribs" : [ <string>, ... ], // Array of Strings |
"awards" : [ |
{ "award" : <string>, year: <number>, by: <string> } // Array of embedded documents |
... |
] |
} |
db.collection.find( { qty: { $gt: 4 } } )
73. Để lọc kết quả bằng cách sử dụng một biến, bạn phải truy cập biến trong toán tửdb.collection.find( { qty: { $gt: 4 } } )
74.Tạo bộ sưu tập
db.collection.find( { qty: { $gt: 4 } } )
75:{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Ví dụ sau đây xác định biến
db.collection.find( { qty: { $gt: 4 } } )
76 trongdb.collection.find( { qty: { $gt: 4 } } )
73 và sử dụng biến để lấy hương vị bánh sô cô la:{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Output:
{ "_id": "apples", "qty": 5 } |
{ "_id": "bananas", "qty": 7 } |
Để xem tất cả các tùy chọn truy vấn có sẵn, xem FindOptions.FindOptions.