Mongoose simple table connection Query

Source: Internet
Author: User

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

Mongoose simple table connection Query

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.