Reputation: 869
I have somewhat large data being stored under one document, it has some rough structure as following:
{
"_id": "rPKzOqhVQfCwy2PzRqvyXA",
"name": "test",
"raw_data": [
{},
...,
{}
],
"records": [
{
"_id": "xyz_1", // customer generated id
...other data
},
{
"_id": "xyz_2", // customer generated id
...other data
},
{},
{},
...
]
}
Now there can be 1000s of records in the document that I need to store from an imported file and each record will have it's own id (programmatically generated). The use case is, after saving this file user wants to do some processing on selected records only (i.e, with id xyz_1
, xyz_2
).
There are a lot of other data can be stored under this single document and I'm not interested to pull all of them while the above use case.
How do I query this document so that I can get the output such as the following:
[
{
"_id": "xyz_1", // customer generated id
...other data
},
{
"_id": "xyz_2", // customer generated id
...other data
}
]
Upvotes: 1
Views: 56
Reputation: 869
as per @mickl's suggestion, my solution is to achieve the output as follows:
db.collection.aggregate([
{ $unwind: "$records" },
{ $replaceRoot: { newRoot: "$records" } },
{ $match: { "_id": { $in: ["xyz_1", "xyz_2"] } } },
])
I think the above solution will go through each document and replace root in each of them, then do match query.
I wanted to search for records from one parent document only, not from all parent documents within the collection. My concern was it should not target other parent documents within a collection so I ended up with the solution, as follows:
db.collection.aggregate([
{ "$match": { "_id": parent_doc_id } },
{ "$unwind": "$records" },
{ "$match": { "records._id": { "$in": ["xyz_1", "xyz_2"] } } },
{ "$group": { "_id": "$_id", "records": { "$push": "$records" } } },
{ "$limit": 1 },
])
Upvotes: 0
Reputation: 49945
You need to run $unwind and $replaceRoot:
db.collection.aggregate([
{ $unwind: "$records" },
{ $replaceRoot: { newRoot: "$records" } }
])
Upvotes: 1