Tôi có dữ liệu với nhiều tài liệu:
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e487"],
"empId" : "2"
"type" : "Admin",
"city" : "Mumbai"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e488"],
"empId" : "3"
"type" : "Admin",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
Tôi muốn lấy dữ liệu theo nhiều điều kiện của mình:
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
Tôi muốn kết quả dưới đây khi chạy điều kiện 1:
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
Khi tôi chạy điều kiện thứ hai:
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
Tôi muốn kết quả ở trên bởi một truy vấn,
Hiện tại tôi đang sử dụng truy vấn tổng hợp dưới đây,
db.emp.aggregate[[
{ $match: { '$and': [
{"type" : "WebUser", "city" : "Pune"},
{"type" : "User", "city" : "Mumbai"}
] } },
{ $group: { _id: 1, ids: { $push: "$empId" } } }
]]
Trên truy vấn làm việc cho điều kiện đầu tiên & thất bại cho người khác. Làm ơn giúp tôi.
Giá trị trường
3 nhỏ hơn {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
4 hoặc lớn hơn {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
5 và → MongoDB Manual{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
6 bằng {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
7 hoặc giá trị trường {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
8 nhỏ hơn {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e489"],
"empId" : "4"
"type" : "User",
"city" : "Mumbai"
}
9.Syntax:
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
1
Truy vấn không thể sử dụng thao tác
3 ngầm vì nó sử dụng toán tử condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
1 nhiều lần. performs a logical db.emp.aggregate[[
{ $match: { '$and': [
{"type" : "WebUser", "city" : "Pune"},
{"type" : "User", "city" : "Mumbai"}
] } },
{ $group: { _id: 1, ids: { $push: "$empId" } } }
]]
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
3 operation on an array of one or more expressions [condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
4, condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
5, and so on] and selects the documents that satisfy all the expressions.Mẹo
Tài liệu về nhà → Hướng dẫn sử dụng MongoDB
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
0Snntax: condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
1
0 thực hiện hoạt động logiccondition 1:- {"type" : "WebUser", "city" : "Pune"} condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
3 trên một mảng gồm một hoặc nhiều biểu thức [condition 1:- {"type" : "WebUser", "city" : "Pune"} condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
4,condition 1:- {"type" : "WebUser", "city" : "Pune"} condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
5, v.v.] và chọn các tài liệu đáp ứng tất cả các biểu thức.condition 1:- {"type" : "WebUser", "city" : "Pune"} condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
Ghi chú
Ví dụ: truy vấn sau luôn tạo ra lỗi nếu
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
2 là {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
3:db.example.find[ { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ]
Truy vấn sau, chứa nhiều biểu thức được cung cấp cho
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
0, có thể gây ra lỗi nếu có bất kỳ tài liệu nào trong đó {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
2 là {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
3:db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
Xem xét truy vấn này:
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
Truy vấn chọn tất cả các tài liệu trong bộ sưu tập
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
7 trong đó:Giá trị trường
8 không bằng{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
9 vàand{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
Trường
8 tồn tại.{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
Truy vấn có thể được viết lại với thao tác
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
3 ngầm kết hợp các biểu thức toán tử cho trường {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
8:db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
Xem xét truy vấn này:
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
Truy vấn chọn tất cả các tài liệu trong bộ sưu tập
{
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
7 trong đó:Giá trị trường
8 không bằng{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
9 vàand{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
Trường
8 tồn tại.or the{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
8 field value is less than{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" }
9.{ "_id" : ObjectId["57b68dbbc19c0bd86d62e486"], "empId" : "1" "type" : "WebUser", "city" : "Pune" } { "_id" : ObjectId["57b68dbbc19c0bd86d62e489"], "empId" : "4" "type" : "User", "city" : "Mumbai" }
Truy vấn có thể được viết lại với thao tác
condition 1:- {"type" : "WebUser", "city" : "Pune"}
condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}
3 ngầm kết hợp các biểu thức toán tử cho trường {
"_id" : ObjectId["57b68dbbc19c0bd86d62e486"],
"empId" : "1"
"type" : "WebUser",
"city" : "Pune"
}
8: