Có các bộ sưu tập và dữ liệu sau đây về chúng
db.a.insert[[
{ "_id" : ObjectId["5b56989172ebcb00105e8f41"], "items" : [{id:ObjectId["5b56989172ebcb00105e8f41"], "instock" : 120}]},
{ "_id" : ObjectId["5b56989172ebcb00105e8f42"], "items" : [{id:ObjectId["5b56989172ebcb00105e8f42"], "instock" : 120}] },
{ "_id" : ObjectId["5b56989172ebcb00105e8f43"], "items" : [{ObjectId["5b56989172ebcb00105e8f43"], "instock" : 80}] }
]]
db.b.insert[[
{ "_id" : ObjectId["5b56989172ebcb00105e8f41"]},
{ "_id" : ObjectId["5b56989172ebcb00105e8f42"]},
{ "_id" : ObjectId["5b56989172ebcb00105e8f43"]},
{ "_id" : ObjectId["5b56989172ebcb00105e8f44"]},
{ "_id" : ObjectId["5b56989172ebcb00105e8f45"]}
]]
thực hiện một tập hợp tra cứu như
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
không mang lại bất kỳ kết quả nào trong hoạt động tra cứu dự kiến. Có bất kỳ hạn chế để sử dụng ObjectID làm so sánh? Trong các tài liệu chính thức không nói về nó và nó hoạt động như một sự quyến rũ với bất kỳ loại dữ liệu nào khác, như chuỗi
- Có tham gia tại MongoDB không?
- Tạo bộ sưu tập mới và sử dụng giai đoạn tổng hợp
0 để tham gia nhiều điều kiện> db.createCollection['users'] > db.createCollection['salaries']
Mã ví dụ:
Có tham gia tại MongoDB không?
Kết hợp MongoDB được thực hiện bằng cách tra cứu. Nó thực hiện tham gia bên ngoài bên trái đến hai hoặc nhiều bộ sưu tập. Nhưng tra cứu chỉ được phép trong các hoạt động tổng hợp. Điều này giống như một đường ống thực hiện các hoạt động truy vấn, bộ lọc và nhóm.
Tham gia nhiều điều kiện bằng toán tử
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
9 trong MongoDBHôm nay, chúng ta sẽ thấy cách tham gia nhiều điều kiện bằng toán tử
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
9 trong MongoDB. Ngoài ra, chúng tôi cũng sẽ khám phá một số ví dụ chứng minh việc sử dụng giai đoạn > db.createCollection['users']
> db.createCollection['salaries']
0 và giai đoạn tổng hợp > db.createCollection['users']
> db.createCollection['salaries']
3.> db.createCollection['users']
> db.createCollection['salaries']
Nếu chúng ta có MongoDB 3.6 trở lên, chúng ta có thể sử dụng toán tử
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
9 > db.createCollection['users']
> db.createCollection['salaries']
6 để tham gia nhiều điều kiện.> db.users.insertMany[
[
{
username: 'userone',
age: 30,
gender: 'Female',
city: 'Lahore',
country: 'Pakistan'
},
{
username: 'usertwo',
age: 35,
gender: 'Male',
city: 'Florida',
country: 'United States'
}
]
]
Đối với điều này, chúng tôi có hai bộ sưu tập có tên
> db.createCollection['users']
> db.createCollection['salaries']
7 và > db.createCollection['users']
> db.createCollection['salaries']
8. Bạn cũng có thể tạo điều đó bằng cách sử dụng các lệnh sau.> db.salaries.insertMany[
[
{
username: 'userone',
salary: 3000
},
{
username: 'usertwo',
salary: 5000
}
]
]
Mã ví dụ để tạo bộ sưu tập:
> db.users.find[].pretty[]
OUTPUT:
{
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
Mã ví dụ để chèn tài liệu vào bộ sưu tập
> db.createCollection['users']
> db.createCollection['salaries']
7:> db.salaries.find[].pretty[]
OUTPUT:
{
"_id" : ObjectId["628deb07c1e812eeeb311437"],
"username" : "userone",
"salary" : 3000
}
{
"_id" : ObjectId["628deb07c1e812eeeb311438"],
"username" : "usertwo",
"salary" : 5000
}
Mã ví dụ để chèn tài liệu vào bộ sưu tập
> db.createCollection['users']
> db.createCollection['salaries']
8:Hiển thị dữ liệu của bộ sưu tập > db.createCollection['users']
> db.createCollection['salaries']
7:
> db.createCollection['users']
> db.createCollection['salaries']
Mã ví dụ:
> db.users.aggregate[[
{
$lookup: {
from: 'salaries',
let: {
user_name: '$username',
user_salary: 3000
},
pipeline: [{
$match: {
$expr: {
$and: [
{ $eq: ['$username', '$$user_name'] },
{ $gte: ['$salary','$$user_salary'] }
]
}
}
}],
as: 'usersalary'
}
}
]].pretty[]
OUTPUT:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
0Đối với ví dụ mã này, chúng tôi tạo một bộ sưu tập mới có tên
> db.salaries.find[].pretty[]
4, hợp nhất hai bộ sưu tập có tên > db.createCollection['users']
> db.createCollection['salaries']
7 và > db.createCollection['users']
> db.createCollection['salaries']
8, sau đó chèn các tài liệu đó vào bộ sưu tập mới được tạo. Sau đó, nhóm theo > db.users.insertMany[
[
{
username: 'userone',
age: 30,
gender: 'Female',
city: 'Lahore',
country: 'Pakistan'
},
{
username: 'usertwo',
age: 35,
gender: 'Male',
city: 'Florida',
country: 'United States'
}
]
]
6 để có được đầu ra mong muốn.- Chúng ta cũng có thể nhận được đầu ra tương tự [như đã nêu ở trên] mà không tạo ra một bộ sưu tập mới. Vì vậy, chúng tôi sử dụng giai đoạn tổng hợp
8, thực hiện một liên minh cho hai bộ sưu tập.> db.salaries.find[].pretty[]
- $ Tra cứu trong MongoDB là gì?
Toán tử tra cứu $ là một toán tử tổng hợp hoặc giai đoạn tổng hợp, được sử dụng để tham gia một tài liệu từ một bộ sưu tập đến một tài liệu của một bộ sưu tập khác của cùng một cơ sở dữ liệu dựa trên một số truy vấn. Cả hai bộ sưu tập nên thuộc về cùng một cơ sở dữ liệu.
$ Expr trong MongoDB là gì?
Mã ví dụ:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
1OUTPUT:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
2$ expr có thể xây dựng các biểu thức truy vấn so sánh các trường từ cùng một tài liệu trong giai đoạn Match $. Nếu giai đoạn $ Match là một phần của giai đoạn tra cứu $, $ expr có thể so sánh các trường bằng các biến LET. Xem thực hiện nhiều lần tham gia và một truy vấn con tương quan với $ Tra cứu cho một ví dụ.
Chúng ta có thể tham gia 2 bộ sưu tập trong MongoDB không?
Để thực hiện MongoDB tham gia hai bộ sưu tập, bạn phải sử dụng toán tử tra cứu $. Nó được định nghĩa là một giai đoạn thực hiện tham gia bên ngoài bên trái với một bộ sưu tập khác và hỗ trợ lọc dữ liệu từ các tài liệu được nối. Ví dụ: nếu người dùng yêu cầu tất cả các lớp từ tất cả học sinh, thì truy vấn dưới đây có thể được viết: sinh viên.
Đây là nơi chúng tôi sử dụng giai đoạn lọc
> db.salaries.insertMany[
[
{
username: 'userone',
salary: 3000
},
{
username: 'usertwo',
salary: 5000
}
]
]
5 và chỉ định các trường nào nên ở đầu ra.Chúng ta có thể sử dụng giải pháp thay thế được đưa ra dưới đây nếu các yêu cầu dự án hạn chế bằng cách sử dụng
> db.salaries.insertMany[
[
{
username: 'userone',
salary: 3000
},
{
username: 'usertwo',
salary: 5000
}
]
]
3, > db.salaries.insertMany[
[
{
username: 'userone',
salary: 3000
},
{
username: 'usertwo',
salary: 5000
}
]
]
4, > db.salaries.insertMany[
[
{
username: 'userone',
salary: 3000
},
{
username: 'usertwo',
salary: 5000
}
]
]
5.Mã ví dụ:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
3OUTPUT:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
4Chúng tôi sử dụng trường
{
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
0 [tùy chọn] để gán các giá trị của các trường cho các biến. Chúng tôi truy cập các biến này trong giai đoạn > db.createCollection['users']
> db.createCollection['salaries']
6, trong đó chúng tôi chỉ định > db.createCollection['users']
> db.createCollection['salaries']
6 để được thực thi trên các bộ sưu tập khác nhau.Lưu ý rằng chúng tôi cũng đang sử dụng giai đoạn
{
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
3 để tận dụng nhà điều hành truy vấn đánh giá có tên {
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
4, so sánh giá trị của các trường.Hơn nữa,
{
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
5 là giai đoạn tổng hợp cuối cùng > db.createCollection['users']
> db.createCollection['salaries']
6 trong > db.createCollection['users']
> db.createCollection['salaries']
6 nơi chúng tôi đang sử dụng toán tử {
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
8 để hợp nhất đầu ra db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
9 với phần tài liệu > db.salaries.find[].pretty[]
0.Chúng tôi chỉ sử dụng toán tử
> db.salaries.find[].pretty[]
1 để tham gia các điều kiện. Bạn cũng có thể sử dụng > db.salaries.find[].pretty[]
2 hoặc cả hai toán tử.Tạo bộ sưu tập mới và sử dụng giai đoạn tổng hợp > db.createCollection['users']
> db.createCollection['salaries']
0 để tham gia nhiều điều kiện
> db.createCollection['users']
> db.createCollection['salaries']
Mã ví dụ:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
5OUTPUT:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
6Chúng tôi sử dụng trường
{
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
0 [tùy chọn] để gán các giá trị của các trường cho các biến. Chúng tôi truy cập các biến này trong giai đoạn > db.createCollection['users']
> db.createCollection['salaries']
6, trong đó chúng tôi chỉ định > db.createCollection['users']
> db.createCollection['salaries']
6 để được thực thi trên các bộ sưu tập khác nhau.Lưu ý rằng chúng tôi cũng đang sử dụng giai đoạn
{
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
3 để tận dụng nhà điều hành truy vấn đánh giá có tên {
"_id" : ObjectId["628deb40c1e812eeeb311439"],
"username" : "userone",
"age" : 30,
"gender" : "Female",
"city" : "Lahore",
"country" : "Pakistan"
}
{
"_id" : ObjectId["628deb40c1e812eeeb31143a"],
"username" : "usertwo",
"age" : 35,
"gender" : "Male",
"city" : "Florida",
"country" : "United States"
}
4, so sánh giá trị của các trường.Mã ví dụ:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
7OUTPUT:
db.b.aggregate[[
{
$lookup:
{
from: "b",
let: { bId: "$_id", qty: 100 },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$items.id", "$$bId" ] },
{ $gte: [ "$instock", "$$qty" ] }
]
}
}
}
],
as: "a"
}
}
]]
6