Подмножество массива в mongodb наличием одного поля - PullRequest
1 голос
/ 14 марта 2019

У меня есть коллекция pub_fulltext_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, в которой есть только поле ссылки с контекстом, например:

{
   _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 },
   ]
}

Я пытаюсь это безрезультатно:

1

pipeline=[db.pub_fulltext_1.aggregate([
{"$match":{"references":{"$elemMatch":{"context":{"$exists": "true"}}}}},
{"$out": "pub_context"}
])]

2

pipeline=[db.pub_fulltext_1.aggregate([
{"$project":{"references":{"$filter":{"input": "$context", "as":"context","cond":{"$exists": "true"}}}}},
{"$out": "pub_context"}
])]

3.

pipeline=[db.pub_fulltext_1.aggregate([
{"$match":{"references.context":{"$exists": "true"}}}}},
{"$out": "pub_context"}
])]

4

pipeline=[db.pub_fulltext_1.aggregate([
{"$match":{"references.context":{"$exists": "true","$ne":"null"}}},
{"$out": "pub_context_3_p"}
])]

5

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"}
        ])]

Есть идеи, как мне это сделать?

1 Ответ

0 голосов
/ 14 марта 2019

Попробуйте это.

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