I have a collection called season that looks like this:
[
{
week: 1,
matchups: [
{
home : ObjectId("1"),
away : ObjectId("2")
},
{
home : ObjectId("3"),
away : ObjectId("4")
},
],
_id: 50fc50c9ce87149ee3000007
},
{
week: 2,
matchups: [
{
home : ObjectId("3"),
away : ObjectId("1")
},
{
home : ObjectId("2"),
away : ObjectId("4")
},
],
_id: 50fc50c9ce87149ee3000008
}
]
I’m trying to get all of the matchups where the home OR away team is ObjectId(“1”). I want the return to look like this:
[
{
week: 1,
matchups: [
{
home : ObjectId("1"),
away : ObjectId("2")
}
],
_id: 50fc50c9ce87149ee3000007
},
{
week: 2,
matchups: [
{
home : ObjectId("3"),
away : ObjectId("1")
}
],
_id: 50fc50c9ce87149ee3000008
}
]
Is it possible to get this result using on db.season.find()?
As long as you have at most one potential
matchupselement to match, you can use the$elemMatchprojection operator to do this:Same caveat as Wes regarding the invalidity of
ObjectId("1").