Answer the question
In order to leave comments, you need to log in
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
For this task, you need to use projection. There are several types of them in Monga:
db.subjects.find(
{"ФизМат.Педагогические науки.B009": {$exists: true}}, // Из всех документов (если у вас их несколько, а не один огромный документ), выбрать те, у которых существует поле "ФизМат.Педагогические науки.B009"
{
_id: 1, // У найденного документа вернуть только поле _id
"ФизМат.Педагогические науки.B009": 1 // и поле "ФизМат.Педагогические науки.B009"
}
)
{
"_id": ObjectId("600fa6f3101fa920a8575e6f"),
"ФизМат": {
"Педагогические науки": {
"B009": {
"code": "B009",
"name": "Подготовка учителей математики",
"max": "140",
"min": " 107",
"minWithQuota": " 95"
}
}
}
}
_id
and ФизМат.Педагогические науки.B009
, no additional transformations will be performed on the document. db.subjects.aggregate(
[
{
$match: { // Эта стадия ищет документы, подходящие под условие
"ФизМат.Педагогические науки.B009": {$exists: true} // В документе есть поле "ФизМат.Педагогические науки.B009"
}
},
{
$project: { // Стадия проекции
_id: 1, // Вернуть id документа
value: "$ФизМат.Педагогические науки.B009" // Вернуть embedded документ в поле value
}
},
])
{
"_id": ObjectId("600fa6f3101fa920a8575e6f"),
"value": {
"code": "B009",
"name": "Подготовка учителей математики",
"max": "140",
"min": " 107",
"minWithQuota": " 95"
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question