doukao8851 2018-11-29 11:56
浏览 244
已采纳

MongoDB查找并迭代与计数

I have a peculiar problem with Mongo.

We have a collection of 800k documents with the following structure.

{
"_id" : ObjectId("5bd844199114bab3b2c19fab"),
"u" : 0,
"c" : 0,
"iden" : "343754856",
"name" : "alan",
"email" : "mkasd@abc.com",
"mobile" : "987654321093456",
"expires" : ISODate("2018-11-29T11:44:25.453Z"),
"created" : ISODate("2018-10-30T11:44:25.453Z")
}

We have indexed iden and name on which we generally query. We tried two types of queries.

  1. db.Collection.find({"iden": "343754856", "name": "alan", "created":
    {"$gt": ....}).count()

    where "created" is an unindexed field.

  2. db.Collection.find({"iden": "343754856", "name": "alan"})

    and iterate over all records to filter based on created.

However, MongoDB seems to be taking enormous amount of time in executing the second query while it was supposed to be an optimization over 1.

Any leads on what is going wrong here? We are using Go library.

  • 写回答

1条回答 默认 最新

  • dshmvqnl98119 2018-11-29 12:20
    关注

    How could the second version be an optimization over the first?

    Your first query retrieves a single number from the MongoDB server: the overall count of the query result. While your second version fetches all matching documents, and you do the counting at the "client" side.

    Believe me that the MongoDB can count internally the result documents just as fast as you could in your Go client. Making the MongoDB server send the results, fetching them and unmarshaling them at the client takes orders of magnitude more time (depending on lot of factors).

    Please note that if you have a composite index containing "iden" and "name", even if you add more filters (like "created" in your example), the index may still be used, but the MongoDB have to iterate over the partial results to apply the rest of the query. To find out if the index is used, please execute the following command:

    db.Collection.find(
        {"iden": "343754856", "name": "alan", "created": {"$gt": ....}
    ).explain()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题