MongoDB的:数组排序 [英] MongoDB: sorted arrays
问题描述
考虑下面的MongoDB的文档:
SomeObject {
nested_objects_ids:
的ObjectId(1 ...),
的ObjectId(2 ...),
...
的ObjectId(N ...)
]
}
nested_object_ids
的长度没有限制。
有一种优雅的方式来继续推动任意值(即的ObjectID)后排序的 nested_object_ids
数组?
感谢您!
有一种优雅的方式来继续推动任意值后排序的
nested_object_ids
数组?
块引用>不幸的是,没有什么我会考虑优雅。
的
$推
命令不能在这里工作。你唯一的选择就是整个子阵列拉入客户端,然后将其与重新写了$设置
。老实说,当谈到与对象数组,MongoDB的功能有限。你可以用
$推
,$拉
更新,你可以对一个对象字段索引,但仅此而已。这很难更新特定子对象。和查询不返回子对象,而是返回整个文档。你可以过滤下来到返回
nested_object_ids
,但你总是能得到一整套那里。一个对你的问题:为什么嵌套的对象需要进行排序
Consider the following MongoDB document:
SomeObject { nested_objects_ids : [ ObjectId( "1..." ), ObjectId( "2..." ), ... ObjectId( "N..." ) ] }
The length of
nested_object_ids
is not limited. Is there an elegant way to keep thenested_object_ids
array sorted after pushing arbitrary values (i.e. ObjectIds)?Thank you!
解决方案Is there an elegant way to keep the
nested_object_ids
array sorted after pushing arbitrary values?Unfortunately, there is nothing I would consider "elegant".
The
$push
command does not work here. Your only option is to pull the entire sub-array into the client and then re-write it with a$set
.Honestly, when it comes to dealing with "arrays of objects", MongoDB has limited functionality. You can update with
$push
,$pull
and you can index on an object field, but that's about it.It's difficult to update a specific sub-object. And querying doesn't return the sub-object, but instead returns the whole document. You could filter it down to returning
nested_object_ids
, but you always get the whole set there.A question for you: why do the nested objects need to be sorted?
这篇关于MongoDB的:数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!