英文:
Mongodb aggregate - Sort then get surrounding documents
问题
I'm providing the translation for the code sections you've mentioned:
我正在尝试编写一个MongoDB聚合查询,以应用两级排序,然后获取已排序集合中特定文档之前和之后的两个文档。首先,我尝试构建一个查询以获取“之后”文档。
文档是无序插入的:
```javascript
db.test.insertMany([
{ name: 'V', type: 'Trap' },
{ name: 'J', type: 'Magic' },
{ name: 'B', type: 'Effect' }
])
要在每个type
内按字母顺序排序,可以执行以下操作:
db.test.aggregate([{ "$sort": { type: 1, name: 1 } }])
结果:
{ name: 'B', type: 'Effect' }
{ name: 'J', type: 'Magic' }
{ name: 'V', type: 'Trap' }
我正在尝试使用以下查询(带有替代)来获取“之后”的文档:
[
{ $sort: { type: 1, name: 1 } },
{
$match: {
name: {
$gt: <name>,
},
type: {
$gte: <type>,
},
},
},
{ $limit: 1 }
]
例如:
文档{ name: 'K', type: 'Trap' }
应该位于J和V之间。
- 上述查询正确返回“V”。
文档{ name: 'C', type: 'Effect' }
应该位于B和J之间。
- 上述查询正确返回“J”。
然而,文档{ name: 'K', type: 'Effect' }
也应该位于B和J之间,因为它应该保留在“Effect”分组中。
- 该查询错误地返回了“V”(应该是J)。
删除$match属性中的任何一个会导致其他情况下的错误结果。似乎无法使用$and /或来在所有情况下获得正确的结果。
对于相反的情况,我的“之前”查询如下(在某些情况下不起作用):
{ $sort: { type: -1, name: -1 } },
{
$match: {
name: { $lt: card.name },
type: { $lte: card.type }
}
},
{ $limit: 1 }
Please let me know if you need any further assistance or specific translations.
<details>
<summary>英文:</summary>
I'm trying to write a mongodb aggregate to apply a 2-level sort and then get the 2 documents before and after a particular document in the sorted set. To start, I'm trying to build a query for the "after" document
Documents are inserted out of order:
db.test.insertMany([
{ name: 'V', type: 'Trap' },
{ name: 'J', type: 'Magic' },
{ name: 'B', type: 'Effect' }
])
To sort alphabetically within each `type` I can do:
db.test.aggregate([{ "$sort": { type: 1, name: 1 } }])
Result:
{ name: 'B', type: 'Effect' }
{ name: 'J', type: 'Magic' }
{ name: 'V', type: 'Trap' }
I'm trying to use the following query (with <param> replacements) to get the "after" document:
[
{ $sort: { type: 1, name: 1 } },
{
$match: {
name: {
$gt: <name>,
},
type: {
$gte: <type>,
},
},
},
{ $limit: 1 }
]
For example:
Document ```{ name: 'K', type: 'Trap' }``` should go between J & V.
- The above query correctly gives "V".
Document ```{ name: 'C', type: 'Effect' }``` should go between B & J.
- The above query correctly gives "J".
However, document```{ name: 'K', type: 'Effect' }``` also needs to go between B & J since it should remain in the "Effect" grouping.
- The query incorrectly returns "V" again (should be J).
Removing either of the $match properties gives incorrect results in other cases. Can't seem to use $and/or to get correct results in all cases either
For the reverse, my "before" query is as following (not working in all cases):
{ $sort: { type: -1, name: -1 } },
{
$match: {
name: { $lt: card.name },
type: { $lte: card.type }
}
},
{ $limit: 1 }
</details>
# 答案1
**得分**: 1
你可以编写一个匹配查询,检查一个“或”条件,其中检查“type”大于给定的类型,或者如果类型等于给定的类型,然后检查“name”是否大于给定的名称。要获取之前的文档,请将“gt”替换为“lt”。
```javascript
db.collection.aggregate([
{ $sort: { type: 1, name: 1 } },
{
$match: {
$or: [
{ type: { $gt: <type> } },
{ $and: [{ type: <type> }, { name: { $gt: <name> } }] }
]
}
},
{ $limit: 1 }
])
英文:
can write a match query which checks an or
condition where check type
is greater than the given type or if the type is equal to the given type then check if the name
is greater than the given name. To get the before document replace gt
with lt
db.collection.aggregate([
{ $sort: { type: 1, name: 1 } },
{
$match: {
$or: [
{ type: { $gt: <type> } },
{ $and: [{ type: <type> }, { name: { $gt: <name> } }] }
]
}
},
{ $limit: 1 }
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论