In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Is there any simple way to do multi-field join in MongoDB? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
JOIN is a very basic operation in SQL, but MongoDB does not provide it directly. You have to use $lookup to deal with it, but it is troublesome to associate multiple fields. For example, this paragraph:
Db.getCollection ('C1'). Aggregate ([
{
$lookup:
{
From: "c2"
LocalField: "user1"
ForeignField: "user1"
As: "R"
}
}
{$unwind: "$R"}
{
$project: {
User1:1
User2:1
Income:1
Output:'$R.output'
Mid: {$cond: [{$eq: ['$user2','$R.user2']}, 1,0]}
}
}
{$match: {"$and": [
{mid: 1}
{income: {$gte: 0.30}}
]
}}
])
This is equivalent to the simple SQL of SELECT S.* FROM C1 S LEFT JOIN C2 R ON S.user1 = R.user1 AND S.user2 = R.user2 WHERE R.income > 0.30.
Because $lookup only supports single-field conditions, multi-field associations use $unwind to split data, $project: to get fields, and to spell association conditions into filter conditions, which is a tedious process.
If there is an aggregator to assist MongoDB, this is very simple, with only one sentence of key code (simpler than SQL):
A1=mongo_open ("mongodb://127.0.0.1:27017/test") 2=mongo_shell (A1, "c1.find ()"). Fetch () 3=mongo_shell (A1, "c2.find ()"). Fetch () 4=A2.join (user1:user2,A3:user1:user2,output). Select (income > 0.30) 5 > A1.close ()
The aggregator SPL is helpful not only for solving JOIN, but also for many difficult calculations of MongoDB. You can refer to the auxiliary MongoDB calculation.
SPL can also be easily embedded in JAVA applications.
The answer to the question about what simple way to do multi-field join in MongoDB is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.