Help in writing queries

I’m studying Mongodb and I have difficulty in writing the queries of the exercize below, namely point 2. May you help me?
Suppose you want to record in a document-based storage solution a list of scientific articles.
Each article is described by a title, a list of authors with respective affiliation and email, the number of pages, an abstract and the actual content.
Content includes paragraphs, in turn containing text, table and images. Paragraphs can also recursively contain subparagraphs, with the same structure.
An article includes also references to other articles (bibliography). As an example:

{_id: doc1,
Title: “X”, Authors: [{name=“A”, affiliation=“PoliTo”}, {name=“B”, affiliation=“KTH”}], Pages:45, Abstract: “…”,
Content: [{section: “S1”, title: “T1”, text: “…”}, {section: “S2”, title: “T2”, text: “…”, imgs: […] }, …],
Bibliography: [{paper: doc2},{paper: doc3},…]
}

Write the following queries:

  1. Find the documents written by at least one author from “PoliTo” that contains at least 20 pages
    and includes a section titled “Big Data” at any of the 3 top-most section levels

db.inventory.find( {
Authors: {$elementMatch: {affiliation=“Polimi”}}, Pages: {$gte 20}, Content: {$or: [ {section: “S1”, title: “Big Data”}, {section: “S2”, title: “Big Data”}, {section: “S3”, title: “Big Data”} ]}
} )

is it correct ?

  1. Find the documents that include a reference to another article written by at least one author from “UniTo”

???

Hello @pierfranco_bellomo, welcome to the MongoDB Community forum!

I have the answers for your two questions and hope you find them useful. Please do try with some sample data and see how they work.

  1. Find the documents written by at least one author from “PoliTo” that contains at least 20 pages and includes a section titled “Big Data” at any of the 3 top-most section levels

I have changed the query little bit. Note that the $elemMatch is not required when comparing a single field from a sub-document element of an array field.

The query filter can be like this:

{ 
    "Authors.affiliation': "PoliTo", 
    Pages: { $gte: 20 }, 
    Content: { $in: [ { section: "S1", title: "Big Data" }, { section: "S2",  title: "Big Data" }, { ... } ] } 
}

  1. Find the documents that include a reference to another article written by at least one author from “UniTo”

This aggregation query uses the $lookup aggregation stage:

db.inventory.aggregate([
{ 
  $lookup: {
      from: "inventory",
      let: { refArticles: "$Bibliography.paper" },
      pipeline: [
          { $addFields: { matches: {  $in: [ "$_id", "$$refArticles" ]  } } },
          { $match: { matches: true, "Authors.affiliation": "UniTo" }},
          { $project: { Authors: 1 } }
      ],
      as: "reference_authors"
  }
},
{ 
  $match: { $expr: { $gt:[ { $size: "$reference_authors" }, 0 ] } }
}
])
1 Like