|
As per the documentation for $map, input needs to be an expression that resolves to an array. In your example, your input is an array that contains one element, namely the string "$name" – but this is no longer a field path.
Changing the name field to be an array does the trick:
> db.foo.drop()
|
> db.foo.insert({name:["Hello"]})
|
WriteResult({ "nInserted" : 1 })
|
> db.foo.aggregate({$project: { "_id": 0, x : {$map: {input: "$name", as: "whee", in: "$$whee"} } }})
|
{ "x" : [ "Hello" ] }
|
If you prefer not to change your schema you can always build the array to use as the input for $map; here's an example using $addToSet:
> db.foo.aggregate(
|
{$group : {
|
_id : "$_id",
|
mapinput : {$addToSet:"$name"}}
|
},
|
{$project : {
|
"_id" : 0,
|
x : {$map: {input: "$mapinput", as: "whee", in: "$$whee"} }
|
}
|
})
|
{ "x" : [ "Hello" ] }
|
|