N
N
NarkoMan012021-01-26 05:07:55
MongoDB
NarkoMan01, 2021-01-26 05:07:55

How to get part of a mongodb document?

Hello!
I want to get a part of the document, namely the object "B009"

tried through:
db.subjects.findOne({'Physical Math.Pedagogical sciences.B183.code':"B183"})
and received the entire document in response(

"ГеоБио": {
  "Педагогические науки": {
    "B001": {
      "code": "B001",
      "name": "Педагогика и психология",
      "max": " 140",
      "min": " 97",
      "minWithQuota": " 91"
    }
  }
},
"ФизМат": {
  "Педагогические науки": {
    "B009": {
      "code": "B009",
      "name": "Подготовка учителей математики",
      "max": "140",
      "min": " 107",
      "minWithQuota": " 95"
    },
    "B183": {
      "code": "B183",
      "name": "Агроинженерия",
      "max": "140",
      "min": " 50",
      "minWithQuota": "none",
      "quotes": []
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-01-26
@NarkoMan01

For this task, you need to use projection. There are several types of them in Monga:

  1. Used in find , findOne , findOneAndDelete , findOneAndReplace , and findOneAndUpdate queries
  2. Used to filter array elements
  3. Used as a separate stage of aggregation

Aggregation type 1 only allows you to hide unnecessary elements from the response. If you use this type, then the request will look like this:
db.subjects.find(
  {"ФизМат.Педагогические науки.B009": {$exists: true}}, // Из всех документов (если у вас их несколько, а не один огромный документ), выбрать те, у которых существует поле "ФизМат.Педагогические науки.B009"
  {
    _id: 1, // У найденного документа вернуть только поле _id
    "ФизМат.Педагогические науки.B009": 1 // и поле "ФизМат.Педагогические науки.B009"
  }
)

The response to such a request:
{
  "_id": ObjectId("600fa6f3101fa920a8575e6f"),
  "ФизМат": {
    "Педагогические науки": {
      "B009": {
        "code": "B009",
        "name": "Подготовка учителей математики",
        "max": "140",
        "min": " 107",
        "minWithQuota": " 95"
      }
    }
  }
}

those. all fields will be hidden, except for _idand ФизМат.Педагогические науки.B009, no additional transformations will be performed on the document.
Type 2 is used to select only the first suitable value from the array, but your items are not designed as an array of documents, so we will skip this type.
3rd type - as a stage of aggregation. Aggregation in mongo is a pretty powerful tool that allows you to group results, transform them, do multilevel searches, etc.
In this case, the request itself will look completely different :
db.subjects.aggregate(
[
  { 
    $match: { // Эта стадия ищет документы, подходящие под условие
      "ФизМат.Педагогические науки.B009": {$exists: true} // В документе есть поле "ФизМат.Педагогические науки.B009"
    } 
  },
  { 
    $project: { // Стадия проекции
      _id: 1, // Вернуть id документа
      value: "$ФизМат.Педагогические науки.B009" // Вернуть embedded документ в поле value
    }
  },
])

The answer in this case would look like this:
{
  "_id": ObjectId("600fa6f3101fa920a8575e6f"),
  "value": {
    "code": "B009",
    "name": "Подготовка учителей математики",
    "max": "140",
    "min": " 107",
    "minWithQuota": " 95"
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question