Get the App
SLTechnology News&Howtos  ›  Database  › 

Detailed explanation of MongoDB Multi-table Association query Operation example

Shulou Source: shulou.com Published: 2022-06-01 03:21:34 11月15日 Update

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.

Tags: Query data association schema model union instance key student method time class update compilation module directory structure result static Zhang San Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi vpn Linux Shulou Tech Info