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

Query function of related tables implemented by Mongodb [population method]

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

Share

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

This paper gives an example to describe the query function of related table realized by Mongodb. Share with you for your reference, the details are as follows:

Population

MongoDB is a non-associative database. But sometimes we still want to quote other documents. This is the opportunity for population to use his talents.

Population replaces a specific path in a document from another document. We can migrate a single file, multiple files, normal objects, multiple ordinary objects, or all objects returned from a query

Populate method

The populate method can be used on document, model, or query objects, which means you can call this method almost anywhere to populate your reference fields.

Of course, the populate method has different parameters on different objects, but it all receives a parameter of option, which you can specify with:

Syntax: Query.populate (path, [select], [model], [match], [options])

Path: the name of the reference field separated by spaces, String or Object type

String type, specify the associated fields to populate, and multiple associated fields to be populated can be separated by spaces.

For the Object type, you encapsulate the parameters of populate into an object. Of course, it could be an array.

Select: Object or String, optionally, specify which fields in the document are populated.

Of type Object, the format is {name: 1, _ id: 0}. 0 means no fill, and 1 means fill.

For the String type, the format, such as "name-_ id", separates the fields with spaces and prefixes the field names with-to indicate that they are not filled. Detailed syntax introduction to query-select

Match: optional, specify additional query criteria

Model: optionally, specify the model of the associated field. If not specified, the ref of Schema will be used.

Options: optional Object to specify additional query options, such as sorting, number limit, etc.

Type of reference field

Currently, Mongoose only supports the following types of reference fields:

ObjectId

Number

String

Buffer

And naturally, the primary key type of the reference document must correspond to the reference field type. In a production environment, it is recommended to use ObjectId for both primary key types and reference types, first, because ObjectId does not contain business meaning, second, ObjectId is unlikely to repeat, and third, because the default primary key type generated by Mongoose is ObjectId, which can reduce a lot of configuration operations.

Examples

Scheme defines and associates attributes first.

AScheme:

Var mongoose = require ('mongoose'); var Schema = mongoose.Schema;var AScheme= new Schema ({name: {type: String, default:'}}, {collection:'a'}); module.exports = mongoose.model ('Aids, CountSchema)

BScheme:

Var mongoose = require ('mongoose'); var Schema = mongoose.Schema;var BScheme= new Schema ({name: {type: String, default:''}, age: {type:Number} aid: {type: mongoose.Schema.ObjectId,// if the _ id type of AScheme is String, which corresponds to String ref:'A'}}, {collection:'b'}) Module.exports = mongoose.model ('baked, CountSchema)

Association query

AModel.find () .populate ({path: 'aid', select: {name: 1pedagogic 0}}) .exec (function (err, a) {if (err) res.send (err); console.log (a);}); / / AModel.find (...) .populate ({path:' aid', match: {age: {$gte: 21}}, select: 'name-_ id', options: {limit: 5}}) .exec ()

For more information, please refer to http://mongoosejs.com/docs/api.html#model_Model.populate.

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