This article mainly introduces to you the relevant information about Mongoose's implementation of virtual field query. The article introduces it in great detail through the example code. It has certain reference learning value for everyone's study or work. Friends who need it follow the small Let’s learn together.
Preface
I don’t know if you know that mongoose provides virtual attributes for the data model, so that it can be more consistent and convenient Read and write model properties, similar to accessors in C# or Java. We know that virtual attributes must not be found in the Query stage, because in fact MongoDB does not store these attributes. But can the query of virtual attributes be implemented through an interceptor?
This question is interesting and quite convenient in many scenarios. For example:
When implementing a violent full-text search, multiple fields need to be matched with a unified query term, which can be abstracted into a virtual attribute;
When you need to query the same complex condition in multiple places, you can use virtual attributes to encapsulate the query condition.
In fact, virtual attribute query and virtual attribute reading and writing are all for code reuse.
Hooks in Mongoose
Mongoose Schema adds .pre and .post hooks to almost all static methods and object methods. These hooks are actually function hooks, implemented using hooks-js.
Example from the official website:
var hooks = require('hooks') , Document = require('./path/to/some/document/constructor'); // Add hooks' methods: `hook`, `pre`, and `post` for (var k in hooks) { Document[k] = hooks[k]; } // Define a new method that is able to invoke pre and post middleware Document.hook('save', Document.prototype.save); // 上述代码在mongoose中实现 ///////////////////////////////////////////////////////////////////// // 下面的代码则是mongoose提供的Hook API // Define a middleware function to be invoked before 'save' Document.pre('save', function validate(next) { // ... });
When Document.save()
is called, the above The validate function will be called back.
Add query hook
Mongoose does not further encapsulate hooks-js, which means that we cannot set hooks for all Query methods, only List the methods that need to be monitored one by one. Of course, this does not affect our code reuse.
// 设置 findOne 和 find 钩子 CompanySchema.pre('findOne', preFind).pre('find', preFind);
The next step is to implement the preFind function.
Implement virtual query
In the hook (preFind), we can change the query conditions to implement a virtual query. It is worth noting that fully controllable Query means that we can implement any form of virtual query.
For example, full text search:
function preFind() { var word = this.getQuery().word; if(word === undefined) return; // 从真实的Query中删掉虚拟属性 delete this._conditions.word; // 构造正则表达式 var regex = new RegExp(word); // 全文检索 this.where({ $or: [{ title: regex }, { content: regex }, { author: regex }] }); }
The above is the detailed content of Detailed explanation of Mongoose's virtual field query implementation method. For more information, please follow other related articles on the PHP Chinese website!