In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to realize the query method of the linked table in mongodb, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Note: only the same database join table query is introduced here, and the cross-library join table query may also be introduced later (because of the change in the company architecture, the join table query may be done later)
There are two kinds of join table queries I use, one is mongoose's populate, and the other is $lookup
1. Populate
Populate uses foreign keys to associate child tables
For example, there is now an order table structure (dynamic foreign key):
Var orderSchema = new mongoose.Schema ({uid: {type: String, required: true}, / / user id amount: {type: Number, required: true}, oType: {type: Number, required: true}, / / order type status: {type: Number, required: true}, / / order status: 1 completed 2 uncompleted 3 invalidated})
User table:
Var userSchema = new mongoose.Schema ({phone: String, status: String, createdAt: Date, updatedAt: Date})
Now I want to query the order table and return the corresponding user phone field
Order.find () .populate ({path: 'uid', model: User, select:' _ id real_name phone bankcard'}) .exec (function (err, order) {/ / order: {/ / uid: {/ / phone: '15626202254records, / / status: "expand", / / createdAt: Date, / / updatedAt: Date / /}, / / amount: 5000, / oType: 2, / / order type / status: 1 / / status of the order: 1 completed 2 uncompleted 3 invalidated / /}})
Here, the uid of the order table points to the _ id field of the user table. Of course, you can also define a foreign key when you create a new table, so I won't go into details here.
2. $lookup
Lookup uses the $lookup attribute of aggregate, which is easy to understand by going directly to the official website.
Orders table
{"_ id": 1, "item": "abc", "price": 12, "quantity": 2} {"_ id": 2, "item": "jkl", "price": 20, "quantity": 1} {"_ id": 3}
Inventory table
{"_ id": 1, "sku": "abc", "description:" product 1 "," instock ": 120} {" _ id ": 2," sku ":" def ", description:" product 2 "," instock ": 80} {" _ id ": 3," sku ":" ijk ", description:" product 3 "," instock ": 60} {" _ id ": 4," sku ":" jkl " Description: "product 4", "instock": 70} {"_ id": 5, "sku": null, description: "Incomplete"} {"_ id": 6} db.orders.aggregate ([{$lookup: {from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs"}])
The item field of order is used as the query condition of inventory table {sku: item} and assigned to the inventory_docs field, but it is worth noting that the type of the two fields must be the same (more than 3.5 seems to be transferable, not tried)
These are all the contents of this article entitled "how to query the tables in the same library with mongodb". Thank you for your reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.