序列化为 mongo 数组的关联容器的每行排序

发布于 2025-01-03 19:36:46 字数 815 浏览 1 评论 0原文

我有一个在 mongo db 数组中序列化的关联容器(具有 C++ 映射的语义)。在我的集合中,我检索包含我的密钥的数组。

db.qrs.find( { "u.i" : 111892 })

我想对我的“地图”的“价值”元素进行排序。例如:

db.qrs.find( { "u.i" : 731612 }).sort( { "u.c" : -1} )

然而,这只是对文档中的数组进行排序,而不是对结果集中的每行进行排序。一个排序行的示例:

 { "_id" : ObjectId("4f2f9acea2b3adfce39ae395"), "i" : 11, "r" : 1, 
"s" : ")n(kQ!$A=@%RPv-SB0dQXh7ME*%lw=!$@(VshFU1H6#b7r*wb()t3Ps!^xQ1rNr7j", "u" : 
[   {   "c" : 7,    "i" : 29518,    "p" : 0 },  { "c" : 1,  "i" : 
924577,     "p" : 3 },  {   "c" : 1,    "i" : 731612,   "p" : 1 } ] }

我希望位置运算符可能会起作用,可惜它没有达到我的预期,我的查询被表述为:

db.qrs.find( { "u.i" : 111892 }).sort( { "u.$.c" : -1} )

那么,我想要的查询是什么样子的?

我能否以某种方式转置结果集以提升与我的谓词匹配的键值对,使其成为结果集顶级文档结构的一部分?

I've got a associative container (with the semantics of a c++ map) serialized in a mongo db array. In my collection I retrieve arrays which contain my key as so.

db.qrs.find( { "u.i" : 111892 })

I would like to sort on the "value" element of my "map". Such as :

db.qrs.find( { "u.i" : 731612 }).sort( { "u.c" : -1} )

However this just sorts the array within the document, rather then sorting per row in the result set. An example sorted row:

 { "_id" : ObjectId("4f2f9acea2b3adfce39ae395"), "i" : 11, "r" : 1, 
"s" : ")n(kQ!$A=@%RPv-SB0dQXh7ME*%lw=!$@(VshFU1H6#b7r*wb()t3Ps!^xQ1rNr7j", "u" : 
[   {   "c" : 7,    "i" : 29518,    "p" : 0 },  { "c" : 1,  "i" : 
924577,     "p" : 3 },  {   "c" : 1,    "i" : 731612,   "p" : 1 } ] }

I had some hope that the position operator might work, alas it does not do what I expected it to , my query was formulated as:

db.qrs.find( { "u.i" : 111892 }).sort( { "u.$.c" : -1} )

So, how does my desired query look like ?

Can I somehow transpose the result set to hoist the key-value pair matching my predicate to become part of the top-level document structure of the result set ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2025-01-10 19:36:46

MongoDB 从不对数组本身进行排序。您将看到文档中存在的数组。

MongoDB 目前不支持您的特定用例。您无法匹配查询中的特定元素,然后单独对该元素进行排序。

您只能通过避免使用数组来解决此问题,例如使用“i”的值作为嵌入元素的键:

{ "_id" : ObjectId("4f2f9acea2b3adfce39ae395"), "i" : 11, "r" : 1, 
"s" : ")n(kQ!$A=@%RPv-SB0dQXh7ME*%lw=!$@(VshFU1H6#b7r*wb()t3Ps!^xQ1rNr7j", "u" : 
{29518:   {   "c" : 7, "p" : 0 },  924577: { "c" : 1,"p" : 3 },  731612: {   "c" : 1,   "p" : 1 } }}

然后您可以进行排序

db.qrs.find({"u.29518":{$exists:true}}).sort({"u.29518.c"-1})

显然这个解决方案可能不适用于您,但我不这样做查看不涉及将数组元素放入专用集合中的任何其他路线(在我看来,这是一个更好的架构决策)。

MongoDB never sorts the array itself. You're seeing the array as it exists in the document.

Your specific use case is not supported by MongoDB at this time. You cannot match a specific element in the query and then sort on that element alone.

You can only fix this by avoiding the use of an array, for example by using the value for "i" as the key of the embedded element :

{ "_id" : ObjectId("4f2f9acea2b3adfce39ae395"), "i" : 11, "r" : 1, 
"s" : ")n(kQ!$A=@%RPv-SB0dQXh7ME*%lw=!$@(VshFU1H6#b7r*wb()t3Ps!^xQ1rNr7j", "u" : 
{29518:   {   "c" : 7, "p" : 0 },  924577: { "c" : 1,"p" : 3 },  731612: {   "c" : 1,   "p" : 1 } }}

You can then sort through

db.qrs.find({"u.29518":{$exists:true}}).sort({"u.29518.c"-1})

Obviously this solution may not be available to you but I don't see any other routes that do not involve putting your array elements in a dedicated collection (which is a better schema decision in my opinion).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文