I perform a query to find one specific record by ID in MongoDB. It works fine, and returns me the following document:
{
key1: "value1",
key2: "value2"
}
Now I need to add to the returned JSON record the key "seeAlso".
This key will contain a sample of 3 other records in the same collection.
The final document should look like it:
{
key1: "value1",
key2: "value2",
seeAlso: [
{doc1},
{doc2},
{doc3},
]
}
The $sample I already know how to do. Gotta add the following to the pipeline:
{
'$sample': {
'size': 10
}
}
What I don’t know yet is how to store the result of this sample in the "seeAlso" key. Such key needs to be created at this point, it doesn’t yet exist.
2
Answers
You may just need to do the
$sample
in a subpipeline. To leave out the original record, add a$match
at the start of the subpipeline to leave out by_id
Mongo Playground
You could do this with $facet with stages:
pipeline1: $match the single document
pipeline2: