I'm aware this is a very simple question, but I don't seem to be able to find a satisfactory answer. Also, I'm a total newbie to Mongo. That clearly doesn't help.
Let's say I have a collection of... users, and I want to apply the $toUpper function in mongo to the name
field of the records with _id ObjectId(1)
and ObjectId(2)
but not to the one with ObjectId(3)
.
So I have this:
{
[
{
"_id": ObjectId("1"),
"name": "user 1",
"address": "Street 1"
},
{
"_id": ObjectId("2"),
"name": "user 2",
"address": "Street 2"
},
{
"_id": ObjectId("3"),
"name": "user 3",
"address": "Street 3"
}
]
}
And I'd like to have:
{
[
{
"_id": ObjectId("1"),
"name": "USER 1",
"address": "Street 1"
},
{
"_id": ObjectId("2"),
"name": "USER 2",
"address": "Street 2"
},
{
"_id": ObjectId("3"),
"name": "user 3",
"address": "Street 3"
}
]
}
I've been playing with Mongo's aggregation (for what I found in google), but the furthest I got is having a query that returns the "name" fields uppered, but:
- It returns all the records in the DB (not only ObjectId("1") and ObjectId("2")
- It doesn't write the changes in the database
- It removes the address field from the returned record (which needless to say, I don't want to lose)
db.user.aggregate([{$project: {"name":{$toUpper:"$name"}}}])
{
"result" : [
{
"_id" : ObjectId("1"),
"name" : "USER 1"
},
{
"_id" : ObjectId("2"),
"name" : "USER 2"
},
{
"_id" : ObjectId("3"),
"name" : "USER 3"
}
],
"ok" : 1
}