排序数组和对象数组MongoDb [英] sort array and Array of Object MongoDb
问题描述
我有一个简单的Collection可以理解MongoDB中的排序
I have a simple Collection for understand sort in MongoDB
我的文档是:
{
"_id" : ObjectId("54b94985d74d670613e4fd35"),
"tag" : [
"A",
"B",
"Z"
]
}
{
"_id" : ObjectId("54b949c9d74d670613e4fd36"),
"tag" : [
"D",
"E",
"F"
]
}
{
"_id" : ObjectId("54b949dfd74d670613e4fd37"),
"tag" : [
"G",
"H",
"I"
]
}
按标签排序时,我有这些结果
When I sort by Tag I Have these results
db.candy.find().sort({tag:1})
{
"_id" : ObjectId("54b94985d74d670613e4fd35"),
"tag" : [
"A",
"B",
"Z"
]
}
{
"_id" : ObjectId("54b949c9d74d670613e4fd36"),
"tag" : [
"D",
"E",
"F"
]
}
{
"_id" : ObjectId("54b949dfd74d670613e4fd37"),
"tag" : [
"G",
"H",
"I"
]
}
代替标签:-1
db.candy.find().sort({tag:-1})
{
"_id" : ObjectId("54b94985d74d670613e4fd35"),
"tag" : [
"A",
"B",
"Z"
]
}
{
"_id" : ObjectId("54b949dfd74d670613e4fd37"),
"tag" : [
"G",
"H",
"I"
]
}
{
"_id" : ObjectId("54b949c9d74d670613e4fd36"),
"tag" : [
"D",
"E",
"F"
]
}
结果非常相似,第一个对象相同,仅更改第二个和第三个对象。
与对象数组相同的结果。
我的问题是:
排序如何工作?
我知道字母A是字母(ASCII码)的第一个字母,而Z是最后一个字母。
mongo检查数组的每个元素(或对象)?
并且为什么当我使用tag:-1和tag:1时数组内部的顺序相同?我希望这样的
The results are very similar, the first object It's the same and Change only the second and the third. Same Results with Array of object. My question is: How it works the sort? I know that the letter A is the first letter of Alphabetic ( ASCII CODE ) and the Z is the last. The mongo check each element ( or object ) of array ? And Why the order inside array is the same when I use tag:-1 and tag:1 ? I expect something like
标签:1
{
"_id" : ObjectId("54b94985d74d670613e4fd35"),
"tag" : [
"A",
"B",
"Z"
]
}
并标记:-1
{
"_id" : ObjectId("54b94985d74d670613e4fd35"),
"tag" : [
"Z",
"A",
"B"
]
}
推荐答案
只需重复并详细说明@marcinn,请回答。
Just to re-iterate and elaborate on @marcinn, answer.
在发出以下语句时,它要求 mongodb
进行 sort
通过查询找到的文档传递给 find()
语句,在这种情况下,集合中的所有文档将由<$ c返回$ c> find()函数,通过标签
字段。
When the below statement is issued, it asks mongodb
to sort
the documents that were found by the query passed to the find()
statement, in this case, all the documents in the collection would be returned by the find()
function, by the tag
field.
点这里要注意的是,字段 tag
的类型为 array
,而不是 simple 字段。
A point to note here is, the field tag
is of type array
and not a simple field.
db.candy.find().sort({tag:1})
如果这是一个简单字段,则文档将按照标记$ c $中的值进行排序c>字段。
If it were a simple field, the documents would have been sorted by the value in the tag
field.
无论如何,mongodb需要一个可以对文档进行排序的值。要获取该字段的值,mongodb将执行以下操作:
Anyway, mongodb needs a value by which it can sort the documents. To get the value of the field, mongodb does the following:
- 检查是否
标签
是一个数组。如果它是一个数组,则需要从该数组中选择一个
元素,该元素的值可以假定为特定文档中
的权重。 - 现在检查如果指定的排序按
升序或降序排列。 - 如果按升序排列,它将在标签
数组中找到最小的元素,否则找到最大的元素 - 从标签数组中选择元素,对于每个文档,都将应用
排序。
- Checks if
tag
is an array. If it is an array, it needs to choose an element from the array whose value it can assume to be the weight of particular document. - Now it checks if the sort specified is in ascending or descending order.
- If it is in ascending order, it finds the smallest element in the tag array, else the largest.
- With the element chosen from the tag array, for each document, the sort is applied.
这里要注意的重要一点是,排序操作仅更改检索到的 root 文档的顺序,换句话说,它的作用类似于
SQL
条款中的子句进行排序。
One important point to note here is that, the sort operation only changes the order of the root documents retrieved, in other words, it acts like an order by
clause in SQL
terms.
它没有更改每个文档的标签数组元素的顺序。
It does not change the order of the tag array elements for each document.
根据经验,使用限制的
find()
查询与之链接的code>, sort
操作不会更改检索到的文档的结构。要操纵文档的结构,您需要执行汇总操作。
As a rule of thumb, a find()
query, with limit
, sort
operations chained to it, does not change the structure of the retrieved documents. To manipulate the structure of the documents you need to perform an aggregate operation.
通过处理每个文档中的字段可以实现您对问题的期望,而这仅是一个
What you expect in your question, is achieved by manipulating the fields in each document, which only an aggregation operation can do.
因此,如果将其汇总为,
So if you aggregate it as,
db.candy.aggregate([
{$match:{"_id":ObjectId("54b94985d74d670613e4fd35")}},
{$unwind:"$tag"},
{$sort:{"tag":-1}},
{$group:{"_id":"$_id","tag":{$push:"$tag"}}}
])
然后您可以得到如下结果:
then you could get your result as:
{
"_id" : ObjectId("54b94985d74d670613e4fd35"),
"tag" : [
"Z",
"A",
"B"
]
}
这篇关于排序数组和对象数组MongoDb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!