根据对孩子父母的参考来查找文档

编程入门 行业动态 更新时间:2024-10-28 02:27:21
本文介绍了根据对孩子父母的参考来查找文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有2个收藏集电影和排行电影.这2个收藏集具有引用关系.

There are 2 collections Movie and Rank Movie .This 2 collections having referenced relationship .

Movie model

Movie model

var mongoose = require('mongoose'); var movieSchema = new mongoose.Schema({ m_tmdb_id: { type: Number, unique: true, index: true }, m_adult: { type: Boolean }, m_backdrop_path: { type: String, }, m_title: { type: Number }, m_genres: { type: Array } }); var MovieModel = mongoose.model('Movie', movieSchema); module.exports = { movie: MovieModel }

Rank movie model

Rank movie model

var mongoose = require('mongoose'); var rankMovieSchema = new mongoose.Schema({ movie: { type: mongoose.Schema.Types.ObjectId, ref: 'Movie', unique: true }, rank: { type: Number }, count: { type: Number } }); var RankMovieModel = mongoose.model('RankMovie', rankMovieSchema); module.exports = { rankmovie: RankMovieModel }

我需要从具有特定标题的电影排行中选择所有项目[电影收集的条件].我该如何实现?

I need to select all the items from the collection rank movie having a particular title[Condition on movie collection ].How can i achieve this?

推荐答案

实际上,最佳"方法是使用 .aggregate() 和 $lookup 以便在匹配条件下合并"数据和过滤".这是非常有效的,因为与发出 .populate() 可以.

Actually the "best" way to do this is rather using .aggregate() and $lookup to "join" the data and "filter" on the match conditions. This is very effective since MongoDB actually performs all of this on the "server" itself, as compared to issuing "multiple" queries as .populate() does.

MovieModel.aggregate([ { "$match": { "m_title": m_title } }, { "$lookup": { "from": RankMovieModel.collection.name, "localField": "_id", "foreignField": "movie", "as": "rankings" }} ])

注意:RankMovieModel.collection.name是从Mongoose注册的Model中获取基础"集合名称的一种好方法.由于操作是在服务器"上进行的,因此MongoDB需要真实的集合名称",因此我们可以硬编码"该名称,也可以仅从在模型本身上注册的信息中获取它.就像这里所做的.

Note: The RankMovieModel.collection.name is a nice way of getting the "underlying" collection name from the Model registered with Mongoose. Since the operation is on the "server", MongoDB needs the "real collection name", so we can either "hardcode" that or just get it from the information registered on the Model itself. As is done here.

如果有很多排名",那么您最好使用 $unwind ,它将为每个相关的排名"项目创建一个文档:

If there are "lots" of rankings, then you are best to use $unwind, which will create a document for each related "rankings" item:

MovieModel.aggregate([ { "$match": { "m_title": m_title } }, { "$lookup": { "from": RankMovieModel.collection.name, "localField": "_id", "foreignField": "movie", "as": "rankings" }}, { "$unwind": "$rankings" } ])

这里还对MongoDB如何处理合并"文档进行了特殊处理,以避免违反16MB BSON限制.因此,实际上,当 $unwind 紧随其后时,就会发生这种特殊的情况 $lookup 管道阶段:

There is also a special handling here of how MongoDB deals with "joining" documents to avoid breaching the 16MB BSON limit. So in fact this special thing happens when $unwind directly follows a $lookup pipeline stage:

{ "$lookup" : { "from" : "rankmovies", "as" : "rankings", "localField" : "_id", "foreignField" : "movie", "unwinding" : { "preserveNullAndEmptyArrays" : false } } }

因此 $unwind 实际上消失了",而是汇总"到 $lookup 本身好像这是一个"操作.这样,我们就不会直接在父文档中创建数组",在极端情况下,如果包含许多相关"项,则该大小会超过16MB.

So the $unwind actually "disappears" and instead is "rolled-up" into the $lookup itself as if this were "one" operation. That way we do not create an "array" directly within the parent document which would cause the size to exceed 16MB in extreme cases with many "related" items.

如果您没有MongoDB支持 $lookup

If you do not have a MongoDB that supports $lookup ( MongoDB 3.2 minunum ) then you could use a "virtual" with .populate() instead (requires Mongoose 4.5.0 minimum). But note this actually performs "two" queries to the server:

首先将虚拟"添加到架构:

First add the "virtual" to the schema:

movieSchema.virtual("rankings",{ "ref": "Movie", "localField": "_id", "foreignField": "movie" });

然后使用 .populate() 发出查询:

Then issue the query with .populate():

MovieModel.find({ "m_title": m_title }) .populate('rankings') .exec()

更多推荐

根据对孩子父母的参考来查找文档

本文发布于:2023-10-26 09:16:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:父母   文档   孩子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!