Answer the question
In order to leave comments, you need to log in
How to filter an array in a collection?
Hello everyone, I have a collection:
{
"_id" : 2,
"name" : "bob",
"age" : 42,
"mess" : [ { "text" : 'hi', "countT" : 8 }, { "text" : 'no', "countT" : 20 } ]
}
{
"_id" : 3,
"name" : "ahn",
"age" : 22,
"mess" : [ { "text" : 'no', "countT" : 8 }, { "text" : 'hi', "countT" : 8 }, { "text" : 'no', "countT" : 20 } ]
}
{
"_id" : 6,
"name" : "abc",
"age" : 43,
"mess" : [ { "text" : 'good', "countT" : 8 }, { "text" : 'no', "countT" : 20 }, { "text" : 'good', "countT" : 8 }, { "text" : 'no', "countT" : 20 } ]
}
Answer the question
In order to leave comments, you need to log in
If I understand you correctly, then I think this will work for you.
db.test.aggregate([{$match : {_id : 6}}, {$redact : {$cond : {if : {$eq : ["$text", "no"]}, then : '$$PRUNE', else : '$$DESCEND'}}}])
, { "_id" : 6, "name" : "abc", "age" : 43, "mess" : [ { "text" : "good", "countT" : 8 }, { "text" : "good", "countT" : 8 } ] }
or so{ "mess" : [ { "text" : "good", "countT" : 8 }, { "text" : "good", "countT" : 8 } ] }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question