Linked table query of basic knowledge of mongodb database



When you do your own project, because the impact of the first contact with mongodb non-relational database and relational database is still in your mind, you always want to do a linked table query, then look at the official website and learn the information, and then use regularity to match the query, make a record here.

1. Mongodb Regular Matching

/* Matching with the $regex field*/
name: {$regex: 'aa', $options: 'i'};
name: {$regex: /aa/, $options: 'i'};
name: {$regex: /aa/i};
/* Direct use of expressions*/
name: {/aa/i}
/* To match with $in, this field must be an array*/
name: {$in: [/aa/]}

2. Connected Table Query

Use $lookup to query linked tables

/* aggregate aggregate operation, $unwind splits the array into individual elements
 * Grouping basis
 * $sum statistics
 * $project filters the return value and returns a field after filtering
 * $match matching condition
 * */
   $lookup: {
    From:'resources', /* the name of the table to connect to*/
    LocalField:'resource_id', /* Fields corresponding to the current table*/
    ForeignField:'_id', /* fields of tables to be joined*/
    As:'resourceInfo'/* field name displayed in query results*/
   $match: {
    'user_id': user_id
   $unwind: '$resourceInfo'
   $sort: {
    _id: -1

3. Summary

Mongodb database is a non-relational and document database. Storing related data in a single document can reduce the number of associated queries between tables.
I first look for information on the Internet to learn, and then I have a great feeling when I study the video on the Internet. At present, there are still some confusions. I will find a better way to insert data.


Above is the whole content of this article. I hope the content of this article has some reference value for your study or work. Thank you for your support to developpaer.