Giả sử chúng ta có một bộ sưu tập có nhiều dữ liệu trong một tài liệu. Chúng tôi có một bộ sưu tập countries
. Chúng tôi sẽ chỉ chọn một vài lĩnh vực chúng tôi muốn,
Bây giờ, tôi chỉ muốn chọn trường name
và chỉ hiển thị kết quả với trường name
Để làm được điều đó, trước tiên chúng ta cần hiểu cú pháp truy vấn mongoDB
Bạn có thể truyền tham số thứ hai cho phương thức find
. Nó cũng sẽ hoạt động cho findOne
Bây giờ, hãy chọn trường name
. Ở đây bạn cần học thêm một thủ thuật nữa, Key sẽ là trường bạn muốn chọn, còn giá trị thì sao? . 0
để bỏ chọn một trường
Hãy xem nó như một ví dụ
Chỉ chọn trường tên quốc gia
Ở đây chúng tôi chỉ chọn thành công trường name
. Chờ đợi?
theo mặc định, truy vấn mongo sẽ luôn bao gồm trường _id
Bỏ chọn bất kỳ trường nào
Hãy bỏ chọn trường _id
, nó cũng giống như vậy,
Hy vọng bạn thấy loạt bài hướng dẫn này hữu ích. Tiếp theo chúng ta sẽ tìm hiểu sâu hơn về series này. Chia sẻ nó với các nhóm của bạn và học tập vui vẻ 🤗
Bạn có thể sử dụng để truy xuất giá trị của các trường có tên chứa dấu chấm [
{ $getField: }
5] hoặc bắt đầu bằng ký hiệu đô la [{ $getField: }
6]Mẹo
Sử dụng để thêm hoặc cập nhật các trường có tên chứa ký hiệu đô la [
{ $getField: }
6] hoặc dấu chấm [{ $getField: }
5]cú pháp
có cú pháp sau
{ $getField: { field: , input: }}
Đồng ruộng
Loại
Sự miêu tả
1{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
Chuỗi
Trường trong đối tượng
2 mà bạn muốn trả về giá trị. { "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1 có thể là bất kỳ giá trị nào phân giải thành hằng chuỗi{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
Nếu
{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1 bắt đầu bằng ký hiệu đô la [_______0_______6], hãy đặt tên trường bên trong biểu thức để trả về giá trị của nó
2{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
Mục tiêu
Vỡ nợ. Một giá trị hợp lệ chứa
1 mà bạn muốn trả về một giá trị. { "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
2 phải giải quyết một đối tượng, { "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1, db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
2 hoặc db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
3. Nếu bị bỏ qua, mặc định là tài liệu hiện đang được xử lý trong đường dẫn []db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
có cú pháp tốc ký sau để truy xuất các giá trị trường từ
{ $getField: }
Đối với cú pháp này, đối số tương đương với giá trị của
{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1 được mô tả ở trênCư xử
Nếu
1 phân giải thành bất kỳ thứ gì khác ngoài hằng chuỗi, thì trả về lỗi{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
Nếu
1 mà bạn chỉ định không có trong đối tượng{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
2 hoặc trong nếu bạn không chỉ định đối tượng{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
2, trả về{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
Nếu
2 đánh giá thành{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1,db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
3 hoặcdb.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
2, trả vềdb.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
2db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
Nếu
2 đánh giá bất kỳ thứ gì khác ngoài đối tượng, thì{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
1,db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
3 hoặcdb.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
2, trả về lỗidb.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
không hoàn toàn đi qua các đối tượng hoặc mảng. Ví dụ: đánh giá giá trị
1 của{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
0 dưới dạng trường cấp cao nhấtdb.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
0 thay vì trường lồng nhaudb.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
2db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
Mẹo
Xem thêm
ví dụ
Các trường truy vấn chứa dấu chấm [{ $getField: }
5]
{ $getField: }
Xem xét một bộ sưu tập
db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
4 với các tài liệu sau{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
Thao tác sau sử dụng toán tử và để tìm sản phẩm nào có
db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
7 lớn hơn db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
8db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: "price.usd" }, 200 ] } } }] ]
Hoạt động trả về kết quả sau
[ { _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 }]
Các trường truy vấn bắt đầu bằng ký hiệu đô la [_______0_______6]
Xem xét một bộ sưu tập
db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
4 với các tài liệu sau{ "_id" : 1, "item" : "sweatshirt", "$price": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "$price": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "$price": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "$price": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "$price": 9.99, qty: 180 }
Thao tác sau đây sử dụng toán tử , , và để tìm sản phẩm nào có
[ { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }]
4 lớn hơn db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
8db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
Hoạt động trả về kết quả sau
[ { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }]
Truy vấn một trường trong tài liệu con
Tạo bộ sưu tập
db.inventory.aggregate[ [ { $match: { $expr: { $gt: [ { $getField: {$literal: "$price" } }, 200 ] } } }] ]
4 với các tài liệu saudb.inventory.insertMany[ [ { "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, "quantity": { "$large": 50, "$medium": 50, "$small": 25 } }, { "_id" : 2, "item" : "winter coat", "price.usd": 499.99, "quantity": { "$large": 35, "$medium": 35, "$small": 35 } }, { "_id" : 3, "item" : "sun dress", "price.usd": 199.99, "quantity": { "$large": 45, "$medium": 40, "$small": 5 } }, { "_id" : 4, "item" : "leather boots", "price.usd": 249.99, "quantity": { "$large": 20, "$medium": 30, "$small": 40 } }, { "_id" : 5, "item" : "bow tie", "price.usd": 9.99, "quantity": { "$large": 0, "$medium": 10, "$small": 75 } }] ]
Thao tác sau đây trả về các tài liệu có số lượng mục
[ { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }]
7 nhỏ hơn hoặc bằng [ { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }]
8db.inventory.aggregate[ [ { $match: { $expr: { $lte: [ { $getField: { field: { $literal: "$small" }, input: "$quantity" } }, 20 ] } } }] ]
Sử dụng các toán tử này để truy vấn bộ sưu tập
Toán tử tìm các giá trị nhỏ hơn hoặc bằng 20
yêu cầu các tham số
1 và{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
2 rõ ràng vì trường{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
7 là một phần của tài liệu phụ[ { _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 }, { _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }]
Làm cách nào để nhận giá trị cột cụ thể trong MongoDB?
Bạn có thể chọn một trường duy nhất trong MongoDB bằng cú pháp sau. db. yourCollectionName. tìm[{"yourFieldName". yourValue},{"yourSingleFieldName". 1,_id. 0}];Làm cách nào để tìm nạp dữ liệu cụ thể từ MongoDB?
Để tìm nạp các giá trị trường cụ thể, hãy sử dụng toán tử inoperator . Thein chọn các tài liệu trong đó giá trị của một trường bằng bất kỳ giá trị nào trong mảng đã chỉ định.Làm cách nào để lấy tên cột trong MongoDB?
Trong MongoDB, không có khái niệm cột vì MongoDB không có lược đồ và không chứa bảng. Nó chứa khái niệm về bộ sưu tập và một bộ sưu tập có các loại tài liệu khác nhau để lưu trữ các mục.