Answer the question
In order to leave comments, you need to log in
Is it possible to get a record with one nested element?
Hello.
I've been digging through the documentation and can't seem to find an answer to a seemingly simple question. Is it possible to pull a record from a collection with a single nested element from the list with one query (aggregation)?
In general, there is a record, in the data field there is a list of nested elements with search keys and other information, there can be up to 8000 such elements in the field. Getting a document and processing it in a loop is not the best option. It would be desirable to receive the document with the superfluous data already cut off.
Document example:
{
"status" : NumberLong(1),
"data" : [
{
"subject" : {
"docNumber" : "0000 000000",
"fname" : "Васильев",
"mname" : "Васильевич",
"lname" : "Васильев"
},
"applications" : [
{
"number" : "ДОК-00022325",
"info" : "..."
}
],
"documents" : [
{
"number" : "ДОК-00022325",
"info" : "..."
}
]
}
]
}
aggregate(
[
{
$match: {
"data.documents.number": "ДОК-00022325"
}
},
{
$unwind: "$data"
},
{
$match: {
"data.documents.number": "ДОК-00022325"
}
}
]
);
Answer the question
In order to leave comments, you need to log in
You can change the schema so that number is the key, like so:
documents: {
"ДОК-00022325": {info:''},
"ДОК-00022326": {},
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question