In SQL I could do something like
SELECT myNum, (myNum+1) as `increment` FROM myTable
effectively doing arbitrary math and other functions and
The new Aggregation Framework in MongoDB 2.2 allows you to add calculated fields via the $project operator. This isn't quite the same as arbitrary functions because you need to use supported operators, but it does provide a good deal of flexibility.
Here is your example of incrementing _id
s into a new myNum
field:
MongoDB shell version: 2.2.0-rc0
> db.test.insert({_id:123});
> db.test.insert({_id:456});
> db.test.aggregate(
{ $project : {
_id : 1,
'myNum': { $add: [ "$_id", 1]}
}}
)
{
"result" : [
{
"_id" : 123,
"myNum" : 124
},
{
"_id" : 456,
"myNum" : 457
}
],
"ok" : 1
}