In modern versions of MongoDB (3.2+) you can use the $filter
operator to select a subset of an array field to return based on a specified condition. Returned elements will be in the original order from the field array.
Example in the mongo
shell:
db.items.aggregate([
{ $match : {
_id: 5
}},
{ $project: {
items: {
$filter: {
input: "$items",
cond: {
"$in": ["$$this", [1, 9, 4]]
}
}
}
}
}])
Note: because the original array in this question has the value 4
twice, the $filter
command will return both occurrences:
{ "_id" : 5, "items" : [ 1, 4, 9, 4 ] }
For an alternative approach which will only return the unique matching items, the $setIntersection
operator could be used:
db.items.aggregate([
{ $match : {
_id: 5
}},
{ $project: {
items: {
$setIntersection: ['$items', [1,4,9]]
}
}}
])
This will return: { "_id" : 5, "items" : [ 1, 4, 9 ] }
.
(original answer from September, 2012 below)
If you want the document manipulation to happen on the server side, you can use the Aggregation Framework in MongoDB 2.2:
db.items.aggregate(
// Match the document(s) of interest
{ $match : {
_id: 5
}},
// Separate the items array into a stream of documents
{ $unwind : "$items" },
// Filter the array
{ $match : {
items: { $in: [1, 9, 4] }
}},
// Group the results back into a result document
{ $group : {
_id: "$_id",
items: { $addToSet : "$items" }
}}
)
Result:
{
"result" : [
{
"_id" : 5,
"items" : [
9,
4,
1
]
}
],
"ok" : 1
}