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

An introduction to Aggregate that is very easy to use in mongodb

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Preface

Aggregate translates to aggregate, but in practice, the experience of using it is very much like a pipe in linux. After each pipe is processed, the result is handed over to the next pipe. Your data is like a stream of water. Finally, through each pipe, you can get the data you want.

What do we usually do with Aggregate?

Aggregate query document

Aggregate averages and other data processing group sum geographic location information $geoNear basically all the query operations of mongodb can be implemented in aggregate, using this is basically a panacea

Here I mainly want to record the technology used by mongodb in the query of geographic location information, which can not only query the distance but also sort it by distance.

$geoNear geolocation information query

First of all, how to store our coordinate data in the database. The type is Array. Remember to add a 2d index and, of course, a 3D index, which is not used yet.

Const storeschema = new mongoose.Schema ({name: {type: String, required: true}, point: {type: Array, required: true}, / / [lon, lat]}); storeschema.index ({point: '2d'}); return mongoose.model (' store', storechema)

And then follow the geographic query code.

This.ctx.model.Store.aggregate ([{$geoNear: {spherical: true, / / spherical whether to calculate the distance according to the shape of the ball distanceMultiplier: 6378137, maxDistance: 10000, near: [lon1, lat1], distanceField: 'dist', key:' point', query: {}},} / / distanceMultiplier this parameter is used to determine the unit of distance you return 6378137 is the maximum distance of m / / maxDistance query / / near center point coordinates / / distanceField distance where the attribute / / key saves the coordinate data / / query your filter condition

One of the interesting things is that

Match, so there is a query attribute here to make up for this regret.

But you can use $match later to filter all the geolocation data found again.

Join table query in $lookup mongodb

$lookup is an attribute that can only be used in newer versions of mongodb. Of course, this attribute is also used in aggregate, which makes up for the regret that tables cannot be joined in mongodb before.

Look at the code.

Await this.ctx.model.MemberInfo.aggregate ([{$match: {store: new ObjectId (store)}}, {$lookup: {from: 'users', localField:' user', foreignField:'_ id', as: 'user'} {$replaceRoot: {newRoot: {$mergeObjects: [{$arrayElemAt: ['$user', 0]},'$ROOT']}, {$match: {'certification.name': {$regex: search}, {$project: {_ id: 1}}])

Memberinfo and user tables here I want to get memberinfo localField: 'user' is the foreign key corresponding to the user table foreignField:' _ id' _ id field his extra properties.

To put it bluntly, only the user's id is stored in my membership table. Now I want to get other information about the user.

Attach the link $lookup

Write at the end

Of course, he is querying the panacea, and of course he supports defining the output of the data.

General operations such as limit $sort

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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