Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of MongoDB Multi-table Association query Operation example

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This paper gives an example of MongoDB multi-table association query operation. Share with you for your reference, the details are as follows:

Multi-table Association query of Mongoose

First, let's recall that the statement of the MySQL multi-table associative query:

Student table:

Calss table:

Query the student name and class data through the classId association of student:

SELECT student.name,student.age,class.name FROM student,class WHERE student.classId = class.id

Mongoose multi-table federated query (or taking well-known students and classes as examples)

Definition of table structure (under schemas directory)

1. Student table (student.js)

Var mongoose = require ('mongoose'); var Schema = mongoose.Schema / * define data schema * / var StudentSchema = new mongoose.Schema ({name: String, calssId: {type: Schema.Types.objectId, ref: 'class'}, age: Number, number: Number, meta: {createAt: {type: Date, default: Date.now ()}, updateAt: {type: Date Default: Date.now ()}} / * update time * /}) Module.exports = StudentSchema

2. Class table (class.js)

Var mongoose = require ('mongoose'); var Schema = mongoose.Schema;/* define data schema * / var ClassSchema = new mongoose.Schema ({name: String, meta: {createAt: {type: Date, default: Date.now ()}, updateAt: {type: Date, default: Date.now ()}} / * update time * /}); module.exports = ClassSchema

Generate Model (under the model directory)

1. Student Model (student.js)

Var mongoose = require ('mongoose'); var StudentSchema = require ('.. / schemas/student'); / * compile model through model to model * / var Student = mongoose.model ('student', StudentSchema); / * Export Student model module * / module.exports = Student

2. Class Model (class.js)

Var mongoose = require ('mongoose'); var ClassSchema = require ('.. / schemas/class'); / * compile model through model to model * / var Class = mongoose.model ('class', ClassSchema); / * Export Class model module * / module.exports = Class

Model carries on the data query operation

1. Add methods of static classes to the compilation of Model

StudentSchema.static = {fetch: function (cb) {return this. Find ({}) .sort ('meta.updateAt') / / sort by updated time}}

two。 Add static class methods to Model

StudentSchema.static ('fetch', function (cb) {return this. Find ({}, cb). Sort (' meta.updateAt')})

3. Directly call the find () method of model

The results of the query are:

[

{

_ id: '5a05222f583e5720b8660191'

Name: 'Zhang San'

Age: 18

Number: 11

ClassId: '5a0036512b740f32e4371e66'

}

{

_ id: '5a05222f583e5720b8660091'

Name:'Li Si'

Age: 19

Number: 11

ClassId: '5a0036512b740f32e1371e66'

}

{

_ id: '5a05222f583e5720b18660191'

Name: 'Zhao Wu'

Age: 17

Number: 11

ClassId: '5a0036512b7420f32e4371e66'

}

]

Multi-table joint query (student corresponding class)

StudentSchema.static = {findStudentWithClass: function (cb) {return this. Find ({}) .populate ('classId') / / Note this is the key to federated queries. Sort (' meta.updateAt') .exec (cb)}}

Query results:

[

{

_ id: '5a05222f583e5720b8660191'

Name: 'Zhang San'

Age: 18

Number: 11

ClassId: {

_ id: '5a0036512b740f32e4371e66'

Name: "one class a year"

}

}

{

_ id: '5a05222f583e5720b8660091'

Name:'Li Si'

Age: 18

Number: 11

ClassId: {

_ id: '5a0036512b740f32e1371e66'

Name: "Class 2, year 2"

}

}

{

_ id: '5a05222f583e5720b18660191'

Name: 'Zhao Wu'

Age: 18

Number: 11

ClassId: {

_ id: '5a0036512b7420f32e4371e66'

Name:'2 classes a year'

}

}

]

As can be seen from the above example, the key to mongoose's multi-table federated query:

1. Data schema structure definition requires the use of the keyword ref to define associations

Var Schema = new mongoose.Schema ({field: {type: mongoose.Schema.Type.ObjectId, ref: 'model'}}); Schema.static = {fetch: function (cb) {return this. Find ({}) .populate (' field') .exec (cb)} var Model = mongoose.Model ('model',Schema)

It is hoped that what is described in this article will be helpful to the programming of MongoDB 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report