最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

关于连接的MongoDB查询

IT培训 admin 5浏览 0评论

关于连接的MongoDB查询

这是一个抽象的例子,但如果有人能帮助我解决它,我会永远感激。

我正在尝试根据相关记录中的字段进行搜索。在此示例中,我们有一个主题层次结构,使用parent_subject字段嵌套。这是我用过的Mongoose定义:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var subjectSchema = new Schema({
   url: { type: String, required: true, unique: true },
   title: { type: String, required: true, unique: true },
   parent_subject: this  
});
module.exports = mongoose.model('Subject', subjectSchema);

我的MongoDB中的一些记录:

{
  "_id" : ObjectId("5a3857f1afeb498c9533aef1"),
  "title" : "Sport",
  "url" : "sport",
  "__v" : 0
},
{
  "_id" : ObjectId("5a3d9a409c0973976f7889c6"),
  "title" : "Cycling",
  "__v" : 0,
  "parent_subject" : ObjectId("5a3857f1afeb498c9533aef1"),
  "url" : "cycling"
},
{
  "_id" : ObjectId("5a3d9a7e9c0973976f788b48"),
  "title" : "Mountain Biking",
  "__v" : 0,
  "parent_subject" : ObjectId("5a3d9a409c0973976f7889c6"),
  "url" : "mountain biking"
}

我想知道如何根据父记录中的字段查询此数据。例如,我想找到所有具有标题为“Sport”的父主题的主题。在SQL中,我会执行以下查询:

SELECT * 
FROM subject 
    INNER JOIN subject AS parentSubject
        ON subject.parent_subject = parentSubject.id
WHERE parentSubject.title = "Sport";

我如何使用MongoDB实现这一目标?

回答如下:

您可以使用mongodb聚合管道并查找此内容

`Subject.aggregate([
  {
   $lookup:
     {
       from: "parentSubject",
       localField: "parent_subject",
       foreignField: "id"
       as: "parentSubject"
     }
},
{$unwind:"$parentSubject"},
{$match:{"$parentSubject.title":"Sport"}}
])`

有关更多参考,请参阅https://docs.mongodb/manual/reference/operator/aggregation/lookup/index.html

关于连接的MongoDB查询

这是一个抽象的例子,但如果有人能帮助我解决它,我会永远感激。

我正在尝试根据相关记录中的字段进行搜索。在此示例中,我们有一个主题层次结构,使用parent_subject字段嵌套。这是我用过的Mongoose定义:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var subjectSchema = new Schema({
   url: { type: String, required: true, unique: true },
   title: { type: String, required: true, unique: true },
   parent_subject: this  
});
module.exports = mongoose.model('Subject', subjectSchema);

我的MongoDB中的一些记录:

{
  "_id" : ObjectId("5a3857f1afeb498c9533aef1"),
  "title" : "Sport",
  "url" : "sport",
  "__v" : 0
},
{
  "_id" : ObjectId("5a3d9a409c0973976f7889c6"),
  "title" : "Cycling",
  "__v" : 0,
  "parent_subject" : ObjectId("5a3857f1afeb498c9533aef1"),
  "url" : "cycling"
},
{
  "_id" : ObjectId("5a3d9a7e9c0973976f788b48"),
  "title" : "Mountain Biking",
  "__v" : 0,
  "parent_subject" : ObjectId("5a3d9a409c0973976f7889c6"),
  "url" : "mountain biking"
}

我想知道如何根据父记录中的字段查询此数据。例如,我想找到所有具有标题为“Sport”的父主题的主题。在SQL中,我会执行以下查询:

SELECT * 
FROM subject 
    INNER JOIN subject AS parentSubject
        ON subject.parent_subject = parentSubject.id
WHERE parentSubject.title = "Sport";

我如何使用MongoDB实现这一目标?

回答如下:

您可以使用mongodb聚合管道并查找此内容

`Subject.aggregate([
  {
   $lookup:
     {
       from: "parentSubject",
       localField: "parent_subject",
       foreignField: "id"
       as: "parentSubject"
     }
},
{$unwind:"$parentSubject"},
{$match:{"$parentSubject.title":"Sport"}}
])`

有关更多参考,请参阅https://docs.mongodb/manual/reference/operator/aggregation/lookup/index.html

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论