doudou5101 2014-03-12 03:03
浏览 79
已采纳

如何从给定字段小写的MongoDB返回文档?

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.

  • 写回答

1条回答 默认 最新

  • douchan7552 2014-03-12 03:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?