Mongoose simple table connection query and Mongoose simple

Source: Internet
Author: User

Mongoose simple table connection query and Mongoose simple

The original article is taken from my front-end blog. You are welcome to visit

Http://www.hacke2.cn

As I mentioned in this article, it is based on Node. js + jade + Mongoose mimic gokk. TV, at that time, the development was stopped because I deeply felt that I was wrong at that time. I should use two schemas instead of an array below to store them. This makes it easy to retrieve data. At that time, paging was quite troublesome, the limit method provided by native cannot be used.

Today I saw a lecture in a book. I tried it and recorded it.

The scenario of our experiment is that there are more than N students in a class, and the class name is first found through the student ID (isn't it tired ?)

Define Schema first

ClazzSchema:

Var mongoose = require ('mongoose') var ClazzSchema = new mongoose. Schema ({clazzName: String}) // other methods omitted...} module. exports = ClazzSchema

StudentSchema:

Var mongoose = require ('mongoose') var StudentSchema = new mongoose. schema ({name: String, clazzID: {type: mongoose. schema. objectId, ref: 'clazz '}) StudentSchema. statics = {findClazzNameByStudentId: function (studentId, callback) {return this. findOne ({_ id: studentid00000000.populate('clazzid'0000.exe c (callback)} // other methods are omitted ..} module. exports = StudentSchema

As you can see, the master needs to set the ClazzID to ref to Clazz, and the dependency is the name of the Model you created. to query Clzz, use populate

Below is the Model

var mongoose = require('mongoose')var ClazzSchema = require('../schemas/clazzSchema')var Clazz = mongoose.model('Clazz',ClazzSchema)module.exports  = Clazz
var mongoose = require('mongoose')var StudentSchema = require('../schemas/studentSchema')var Student = mongoose.model('Student',StudentSchema)module.exports  = Student

Look at test. js.

Var mongoose = require ('mongoose') var Clazz = require ('. /models/clazzModel ') var Student = require ('. /models/studentModel ') // var db = mongoose. createConnection ('localhost', 'okk') mongoose. connect ('mongodb: // localhost/test')/* var clazz = new Clazz ({clazzName: 'software 2 class'}); clazz. save (function (argument) {console. log ('true') ;}); * // * var student = new Student ({name: 'hacke2', clazzID: '542b5fcc49df6e741d8d15f5 '}) student. save (function (err) {console. log ('true');}) */Student. findClazzNameByStudentId ('542b600a683d59a80d4ee632', function (err, student) {if (err) console. log (err); console. log (student. clazzID. clazzName );})

Two classes were added before: Software Class 1 and software class 2.

When hacke2 is added, the classID is set to software Class 2. When hacke2 is updated, the key Class is automatically queried.

{_ Id: 542b600a683d59a80d4ee632, name: 'hacke2', clazzID: {_ id: Role, clazzName: 'software 2 ban', _ v: 0}, _ v: 0}

End from http://www.hacke2.cn


How to query tables connected to three tables

Select *, 'view' as sate from
Union all
Select *, 'Download' as state from B
Union all
Select *, 'invitation' state from c

Join the where clause with three tables

* Replace it with the specific field you want, but the number type must be the same. Actually, it cannot be null.

Database Connection Table query Problems

Simple:
Select sendername = (select name from Table B where senderid = ID), Region ename = (select name from Table B WHERE RECEIVEID = ID), MailTitle from Table
======
Find the problem.
This query is the most common method to query the code dictionary.
=====
The stored procedure is the most common practice of inserting records and returning automatically generated code.
====
Are you in touch with these two questions? It cannot be seen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.