In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Source: http://stackoverflow.com/questions/29392169/populating-field-values-for-referred-documents-in-aggregate-call-in-mongoose.
[abstract]
Mongodb as a distributed file storage database, it is not easy to implement multi-table association JOIN query. However, through the aggregator SPL language, the foreign key association query of multiple tables in Mongodb can be as convenient and easy as the relational database.
MongoDB is a distributed file storage database, and it is also one of the most relational databases in nosql. But mongodb is designed in document mode, which means that documents in a collection can have the same or different fields, so it is not satisfactory in the aspect of multi-table association that relational databases are very good at. If you use Mongodb's own API, you need to hard-code to achieve foreign key association, which is not intuitive and difficult. In this case, you can use the aggregator SPL language to achieve, the following example.
Collection UserCourseProgress records the relationship between the user and the course, and its courseid field is a foreign key, pointing to the _ id field of Collection Course. The number of people in each course needs to be counted, and the course name needs to be displayed using the title field of Course.
UserCourseProgressCourse {"userId": "U01"
"courseid": "C01"
"timespent": 6000
Score:99}
{"userId": "U02"
"courseid": "C01"
"timespent": 6000
Score:99}
{"userId": "U03"
"courseid": "C01"
"timespent": 6000
Score:99}
{"userId": "U04"
"courseid": "C01"
"timespent": 6000
Score:99}
{"userId": "U05"
"courseid": "C01"
"timespent": 6000
Score:99}
{"userId": "U01"
"courseid": "C02"
"timespent": 6000
Score:99}
{"userId": "U02"
"courseid": "C02"
"timespent": 6000
Score:99}
{"userId": "U03"
"courseid": "c03"
"timespent": 6000
Score:99} {"_ id": "c01"
"title": "Japanese159"
"description": "Japanese base"
"category": "language"}
{"_ id": "C02"
"title": "Chinese200"
"description": "Chinese middle"
"category": "language"}
{"_ id": "c03"
"title": "Political science 280"
"description": "Political middle"
"category": "politics"}
{"_ id": "c04"
"title": "EE490"
"description": "electronic engineering hign"
"category": "Electronic"}
The code to use the aggregator SPL is as follows:
A1=mongo_open ("mongodb://localhost:27017/local?user=test&password=test") 2=mongo_shell (A1, "UserCourseProgress.aggregate ([{$group: {_ id: {'primary':'$courseid'},' popularityCount': {$sum: 1}, {$sort: {'popularityCount':-1}}, {$project: {_ id:0,'courseid':'$_id.primary','popularityCount':1}}]) 3=mongo_shell (A1," Course.find ( {title:1}) ") .fetch () 4=A2.switch (courseid,A3:_id) 5=A4.new (popularityCount,courseid.title) 6=mongo_close (A1)
A1: connect MongoDB, the connection word format is mongo://ip:port/db?arg=value& ….
A2: count the number of people in each course. Here, the MongoDB aggregate function aggregate is used to fetch numbers in UserCourseProgress, and the parameters are summary expressions that follow the mongodb specification. The calculated result is in-memory data, as follows:
A3: use the find function to fetch numbers from Course, and the filter condition is empty. The result of Find is cursors. Because of the small number of courses, cursors can be read into memory with the fetch function. The results are as follows:
A4: use the switch function to switch the foreign key in A3 to the record in A2. The result is as follows:
A5: access memory by object to form a new two-dimensional table. The result is as follows:
A6: close the mongodb connection.
Through the above example, we can see that with the help of the aggregator SPL language, the foreign key association query of multiple tables in Mongodb can be as convenient, easy and intuitive as a relational database, and there is no need to worry about the impact of weak association between tables because Mongodb is a non-relational database.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.