gpt4 book ai didi

mongodb - 如何在pymongo中使用map-reduce获取字段的最大值文档?

转载 作者:行者123 更新时间:2023-12-01 23:24:10 24 4
gpt4 key购买 nike

如何在 pymongo 中使用 map-reduce 查找具有最大 uid 字段的文档?

我尝试了以下方法,但打印出空白:

from pymongo import Connection
from bson.code import Code

db = Connection().map_reduce_example
db.things.insert({
"_id" : "50f5fe8916174763f6217994",
"deu" : "Wie Sie sicher aus der Presse und dem Fernsehen wissen, gab es in Sri Lanka mehrere Bombenexplosionen mit zahlreichen Toten.\n",
"uid" : 13,
"eng" : "You will be aware from the press and television that there have been a number of bomb explosions and killings in Sri Lanka."
})

db.things.insert({
"_id" : "50f5fe8916234y0w3fvhv234",
"deu" : "Ich bin schwanger foo bar.\n",
"uid" : 14,
"eng" : "I am pregnant foobar."
})

db.things.insert({
"_id" : "50f5fe8916234y0w3fvhv234",
"deu" : "barbar schwarz sheep, haben sie any foo\n",
"uid" : 14,
"eng" : "barbar black sheep, have you any foo"
})

m = Code("function () {emit(this.uid,{'uid':this.uid,'eng':this.eng})}")

r = Code("function (key, values) {var total = 0;for (var i = 0; i < values.length; i++) {total += values[i];}return total;}")


result = db.things.inline_map_reduce(m, r)

for r in result:
print

示例文档如下所示:

{ 
"_id" : ObjectId("50f5fe8916174763f6217994"),
"deu" : "Wie Sie sicher aus der Presse und dem Fernsehen wissen, gab es mehrere Bombenexplosionen mit zahlreichen Toten.\n",
"uid" : 13,
"eng" : "You will be aware from the press and television that there have been a
number of bomb explosions and killings."
}

最佳答案

您可以使用find_one通过按该字段降序排序来查找具有最大 uid 的文档:

db.things.find_one(sort=[("uid", -1)])

或使用定义的常量:

db.things.find_one(sort=[("uid", pymongo.DESCENDING)])

关于mongodb - 如何在pymongo中使用map-reduce获取字段的最大值文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com