【发布时间】:2012-08-12 04:32:25
【问题描述】:
我有一个 Mongo 查找查询,可以很好地从大型文档中提取特定字段,例如...
db.profiles.find(
{ "profile.ModelID" : 'LZ241M4' },
{
_id : 0,
"profile.ModelID" : 1,
"profile.AVersion" : 2,
"profile.SVersion" : 3
}
);
...这会产生以下输出。请注意文档中 SVersion 是如何出现在 AVersion 之前的,即使我的投影在 SVersion 之前要求 AVersion。
{ "profile" : { "ModelID" : "LZ241M4", "SVersion" : "3.5", "AVersion" : "4.0.3" } }
{ "profile" : { "ModelID" : "LZ241M4", "SVersion" : "4.0", "AVersion" : "4.0.3" } }
...问题是我希望输出是...
{ "profile" : { "ModelID" : "LZ241M4", "AVersion" : "4.0.3", "SVersion" : "3.5" } }
{ "profile" : { "ModelID" : "LZ241M4", "AVersion" : "4.0.3", "SVersion" : "4.0" } }
我需要做什么才能让 Mongo JavaScript shell 以我指定的字段顺序显示我的查询结果?
【问题讨论】:
-
您的查询在哪里?您是否考虑过使用排序。
-
我的查询在上面。在一行中: db.profiles.find( { "profile.ModelID" : 'LZ241M4' }, { _id : 0, "profile.ModelID" : 1, "profile.AVersion" : 2, "profile.SVersion" : 3});我没有考虑 sort(),但是对“行”进行排序,我正在尝试对“列”进行排序。
-
对不起,我不知道我是怎么错过的。我想你已经知道了,但是 mongodb 中有“列”之类的东西。这是您的整个文档还是个人资料是较大文档的子集?
-
"profile" 是较大文档的子文档。我在“行”和“列”周围加上引号,因为我知道 Mongo 是关于文档和字段的,但是为了本次对话的目的,“字段”和“列”在概念上是相似的。我的投影按顺序要求“字段” { _id : 0, "profile.ModelID" : 1, "profile.AVersion" : 2, "profile.SVersion" : 3 } 但 Mongo JavaScript shell 不产生输出以这种方式处理每个文档。我正在寻找一种方法来重新排列每个文档的输出。感谢您的帮助
标签: mongodb