简体   繁体   English

MongoDB 从不在其他集合中的集合中获取文档

[英]MongoDB get documents from a collection not in other collection

I'm trying to get the documents from a collection where aren't in other collection (the common NOT IN clause in SQL).我正在尝试从不在其他集合中的集合中获取文档(SQL 中常见的 NOT IN 子句)。

If I run the next query:如果我运行下一个查询:

db.Companies_Movies.aggregate([
    {
        $project: 
        {
            "CompanyList.Movies.Code" : 1
        }
    },
    {
        $match: 
        {
             "CompanyList.CodeCompany": "23"
        }
    },
    {   
        $lookup:    
        {
            from: "Movies",
            localField: "CompanyList.Movies.Code",
            foreignField: "Movie.Code",
            as: "matched_docs"
        }
    }
]);

This query shows the movies includes in CompanyList.Movies.Code and in Movie.Code.此查询显示 CompanyList.Movies.Code 和 Movie.Code 中包含的电影。 Good.好的。 But I just have the rest of movies includes in CompanyList.Movies whose codes aren't included in Movie.Code.但我只有 rest 的电影包含在 CompanyList.Movies 中,其代码未包含在 Movie.Code 中。

As Nikos Tsagkas said in Get "data from collection b not in collection a" in a MongoDB shell query it should be sufficient to include the following sentence:正如 Nikos Tsagkas 在Get "data from collection b not in collection a" in a MongoDB shell 查询中所说,包含以下句子就足够了:

    {
        $match: { "matched_docs": { $eq: [] } }
    }

But when I run my final code, it doesn't returns anything:但是当我运行我的最终代码时,它不会返回任何内容:

    db.Companies_Movies.aggregate([
    {
        $project: 
        {
            "CompanyList.Movies.Code" : 1
        }
    },
    {
        $match: 
        {
            "CompanyList.CodeCompany": "23"
        }
    },
    {
        $lookup:    
        {
            from: "Movies",
            localField: "CompanyList.Movies.Code",
            foreignField: "Movie.Code",
            as: "matched_docs"
        }
    },
    {
        $match: { "matched_docs": { $eq: [] } }
    }
    ]);

There are 59 documents that are not returned by this code.此代码未返回 59 个文档。


This is my pipeline I've created in MongoDB Compass after Tom's changes and it still doesn't work:这是我在 Tom 更改后在 MongoDB Compass 中创建的管道,但它仍然无法正常工作:

[{
    $match: 
     {
        'CompanyList.CodeCompany': '23'
     }
     },
     {
          $lookup: 
          {
               from: 'Movies',
               localField: 'CompanyList.Movies.Code',
               foreignField: 'Movie.Code',
               as: 'docs'
          }
     }, 
     {
          $project: 
          {
               'CompanyList.Movies.Code': 1,
               'CompanyList.CodeCompany': 1
          }
     }, 
     {
          $match: 
          {
               docs:{  $eq: [] }
          }
    }]

If I delete the $project, it not works either.如果我删除 $project,它也不起作用。


Sample Data (reduced)样本数据(减少)

Companies_Movies collection:公司_电影合集:

{
_id:ObjectId("61bf47b974641866e1244e65"),
"CompanyList": {
    "CodeCompany": "23",
    "NameCompany": "Company Name Entertainment",
    "Movies": [{
        "Code": "123",
        "Name": "Title 1",
        "Order": 1,
        "UserDescription": null
    }, {
        "Code": "124",
        "Name": "Title 2",
        "Order": 2,
        "UserDescription": null
    }, {
        "Code": "125",
        "Name": "Title 3",
        "Order": 3,
        "UserDescription": null
    }],
    "DateInserted": {
        "$date": "2021-12-13T17:30:06.824Z"
    }
  }
}

Movies collection:电影合集:

[{
_id:ObjectId("61bf57bc9d1f93b7ae5fa785"),
"Movie": {
    "Code": "123",
    "OriginalTitle": "Title 1",
    "Year": 2021
 },
_id:ObjectId("61bf57bc9d1f93b7ae5fa786"),
"Movie": {
    "Code": "124",
    "OriginalTitle": "Title 2",
    "Year": 2021
 },
_id:ObjectId("61bf57bc9d1f93b7ae5fa787"),
"Movie": {
    "Code": "125",
    "OriginalTitle": "Title 3",
    "Year": 2021
 },
_id:ObjectId("61bf57bc9d1f93b7ae5fa788"),
"Movie": {
    "Code": "126",
    "OriginalTitle": "Title 4",
    "Year": 2021
 }
}]

Anyone know what might be happening?有谁知道可能会发生什么?

Thanks to everyone.谢谢大家。

This is simply caused by your $project stage, after you run:这只是由您的$project阶段引起的,在您运行后:

{
  $project: {
    "CompanyList.Movies.Code" : 1
  }
},

You're data will look like this:您的数据将如下所示:

{
   CompanyList: [
       {
          Movies: { code: "123", ... other fields } 
       }
   ]
}

Now you're trying to match "CompanyList.CodeCompany": "23" , but the field CodeCompany simply does not exist anymore as you did not provide it in the project stage.现在您正在尝试匹配"CompanyList.CodeCompany": "23" ,但CodeCompany字段根本不存在,因为您没有在项目阶段提供它。

So just change you're projection stage to include fields you will use in later stages:因此,只需更改您的投影阶段以包含您将在稍后阶段使用的字段:

{
  $project: {
    "CompanyList.Movies.Code" : 1,
    "CompanyList.CodeCompany": 1
  }
},

Tom.汤姆。 Thanks for your help.谢谢你的帮助。

Your solution not works.您的解决方案不起作用。

This is my pipeline created in MongoDB Compass after your changes:这是我在更改后在 MongoDB Compass 中创建的管道:

[{
    $match: 
     {
        'CompanyList.CodeCompany': '23'
     }
     },
     {
          $lookup: 
          {
               from: 'Movies',
               localField: 'CompanyList.Movies.Code',
               foreignField: 'Movie.Code',
               as: 'docs'
          }
     }, 
     {
          $project: 
          {
               'CompanyList.Movies.Code': 1,
               'CompanyList.CodeCompany': 1
          }
     }, 
     {
          $match: 
          {
               docs:{  $eq: [] }
          }
    }]

If I delete the $project, it not works either.如果我删除 $project,它也不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据数组集合中的值获取 _id 并插入到 mongodb 集合中 - get _id based on value from a collection for a array and insert in mongodb collection 查询具有JSON文档数组的MongoDB集合 - Querying MongoDB collection having array of JSON documents Mongodb从收藏中搜索 - Mongodb Search from Collection 从集合中删除文档,并从另一个集合中的数组中删除ID - Delete documents from collection and remove Ids from an array in another collection Mongodb 2 集合与集合数组 - Mongodb 2 collection with collection array mongodb - 从集合中的两个 arrays 中获取一个数组 - mongodb - Get one array from two arrays in collection 为mongoDB集合中的所有文档添加2dsphere索引 - add 2dsphere index for all documents in mongoDB collection MongoDB:使用pymongo删除一个集合中多个文档中的数组值 - MongoDB: deleting an array value in multiple documents in one collection using pymongo 如何跨 mongodB 集合中的所有文档聚合映射键? - How to aggregate over map keys across all the documents in mongodB collection? 使用包含(子)文档数组的文档查询MongoDB集合,其中该数组中的所有文档均与条件匹配 - Querying a MongoDB collection with documents containing an array of (sub)documents where ALL documents in the array match a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM