通过一个字段的存在来子集mongodb中的数组

通过一个字段的存在来子集mongodb中的数组,mongodb,pymongo,Mongodb,Pymongo,我有一个收藏pub\u fulltext\u 1如下: { _id: 0, author:Jose Fernandez, year: 2019, reference: [ { item_id: 43, author: Alberto Perez, year: 1910, context: some text }, { item_id: 44, author: Lucas Leys, year: 1990, context: some text },

我有一个收藏
pub\u fulltext\u 1
如下:

{
   _id: 0,
   author:Jose Fernandez,
   year: 2019,
   reference: [
     { item_id: 43, author: Alberto Perez, year: 1910, context: some text },

     { item_id: 44, author: Lucas Leys, year: 1990, context: some text },

     { item_id: 45, author: Johan Ortiz, year: 2005}
   ]
}
{
   _id: 1,
   author: Ramiro Ramirez,
   year: 2015,
   reference: [
     { item_id: 68, author: Mats Valk, year: 1993, context: some text },

     { item_id: 74, author: Robert Lucas, year: 1976, context: some text },

     { item_id: 80, author: Mark Ljumberg, year: 2005, context: some text}
   ]
}
{
   _id: 2,
   author: Feliks Zemdges,
   year: 2018,
   reference: [
     { item_id: 1, author: Gan Zandhi, year: 2015},

     { item_id: 2, author: Dayan Wojung, year: 1976, context: some text },

     { item_id: 80, author: Mats Valk, year: 2014}
   ]
}
我需要创建一个新的集合
pub_context
,该集合只有带有context的引用字段,如下所示:

{
   _id: 0,
   author:Jose Fernandez,
   year: 2019,
   references: [
     { item_id: 43, author: Alberto Perez, year: 1910, context: some text },

     { item_id: 44, author: Lucas Leys, year: 1990, context: some text }
   ]
}
{
   _id: 1,
   author: Ramiro Ramirez,
   year: 2015,
   references: [
     { item_id: 68, author: Mats Valk, year: 1993, context: some text },

     { item_id: 74, author: Robert Lucas, year: 1976, context: some text },

     { item_id: 80, author: Mark Ljumberg, year: 2005, context: some text}
   ]
}
{
   _id: 2,
   author: Feliks Zemdges,
   year: 2018,
   references: [
     { item_id: 2, author: Dayan Wojung, year: 1976, context: some text },
   ]
}
我正在尝试,但没有结果:

一,

二,

三,

四,

五,

你知道我该怎么做吗?

试试这个

db.collection.aggregate([
{
  $project: {
        author:1,
        year:1,
     reference: {
        $filter: {
           input: "$reference",
           as: "item",
           cond: {$ifNull:["$$item.context",null]}
        }
     }
  }
}
])
试试这个

db.collection.aggregate([
{
  $project: {
        author:1,
        year:1,
     reference: {
        $filter: {
           input: "$reference",
           as: "item",
           cond: {$ifNull:["$$item.context",null]}
        }
     }
  }
}
])

它保存所有文档,有上下文和无上下文。它保存所有文档,有上下文和无上下文。
pipeline=[db.pub_fulltext_1.aggregate([
{"$match":{"references.context":{"$exists": "true"}}}}},
{"$out": "pub_context"}
])]
pipeline=[db.pub_fulltext_1.aggregate([
{"$match":{"references.context":{"$exists": "true","$ne":"null"}}},
{"$out": "pub_context_3_p"}
])]
pipeline=[db.pub_fulltext_1.aggregate([
{
  "$project": {
        "author":1,            
        "year":1,
        "references": {
        "$filter": {
        "input": "$references",
        "as": "item",
        "cond": {"$ifNull":["$$item.context","null"]}
        }
        }
        }
        },
        {"$out": "pub_context_17_p"}
        ])]
db.collection.aggregate([
{
  $project: {
        author:1,
        year:1,
     reference: {
        $filter: {
           input: "$reference",
           as: "item",
           cond: {$ifNull:["$$item.context",null]}
        }
     }
  }
}
])