Reputation: 331
I have a query
collection in mongodb which contains document in the below format :
{
_id : ObjectId("61aced92ede..."),
query : "How to solve...?",
answer : []
is_solved : false
}
Now, I want to filter the documents with the following condition
filter all documents that are not solved. (is_solved : true
)
filter "n" number of document that are solved.
So, That result will have all unsolved documents and only 10 solved documents in an array.
Upvotes: 0
Views: 78
Reputation: 15177
You can use this aggregation query:
$match
and $limit
the solved documents.$concatArrays
.db.collection.aggregate([
{
"$facet": {
"not_solved": [
{
"$match": {
"is_solved": false
}
}
],
"solved": [
{
"$match": {
"is_solved": true
}
},
{
"$limit": 10
}
]
}
},
{
"$project": {
"result": {
"$concatArrays": [
"$not_solved",
"$solved"
]
}
}
}
])
Example here where I've used $limit: 1
to see easier.
Also, if you want, you can add $unwind
at the end of the aggregation to get values at the top level like this example
Upvotes: 1