Question

I am using the Mongo extension of PHP.

I have a collection of two-level documents in MongoDB, and each document has a "email" field at a second level (i. e., "user_data.email" field). I want to get a MongoCursor loaded with documents, each having this field in lowercase. Is this possible?

The dataset is ~100000 documents and I have already profiled my application and know that strtolower() over the results at PHP side is a bottleneck.

Was it helpful?

Solution

Bit of a pain that. maybe mapReduce?

db.collection.mapReduce(
    function() {
        emit( this._id, values: {
            email: this.user_data.email.toLowerCase(),
            other: this.other_field.toLowerCase()
        });
    },
    function(){},
    {
        "out": { "replace": "newCollection" }
    }
)

And the get the results from the new collection with .find().

Or you could wait for 2.6, where aggregate does return a cursor

db.collection.aggregate([
    { "$project": { "email": { "$toLower": "$user_data.email" } } }
])

Really not sure of your use case. E-mail should be considered case insensitive, so any matching should take that into consideration.

But if comparison is your thing, why not use strcasecomp ?

At the end of the day, this is probably something that you should update in your data, and then make sure that new records are always inserted with the email address in lowercase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top