Answer the question
In order to leave comments, you need to log in
How to lower the priority of empty fields in MongoDB when sorting?
Good afternoon. I have a problem with sorting. I need to sort by the fields f0, f1, f2, but do not take into account the case of the character, and put records that do not have a field at the end of the list.
Here is the test data
db.getCollection("test").insert({f0: "A", f1: "A", f2: "A"});
db.getCollection("test").insert({f0: "A", f1: "A", f2: "a"});
db.getCollection("test").insert({f0: "A", f1: "A", f2: "B"});
db.getCollection("test").insert({f0: "A", f1: "A", f2: "b"});
db.getCollection("test").insert({f0: "A", f1: "A"});
db.getCollection("test").insert({f0: "A", f1: "a", f2: "A"});
db.getCollection("test").insert({f0: "A", f1: "a", f2: "a"});
db.getCollection("test").insert({f0: "A", f1: "a", f2: "B"});
db.getCollection("test").insert({f0: "A", f1: "a", f2: "b"});
db.getCollection("test").insert({f0: "A", f1: "a"});
db.getCollection("test").insert({f0: "A", f1: "B", f2: "A"});
db.getCollection("test").insert({f0: "A", f1: "B", f2: "a"});
db.getCollection("test").insert({f0: "A", f1: "B", f2: "B"});
db.getCollection("test").insert({f0: "A", f1: "B", f2: "b"});
db.getCollection("test").insert({f0: "A", f1: "B"});
db.getCollection("test").insert({f0: "A", f1: "b", f2: "A"});
db.getCollection("test").insert({f0: "A", f1: "b", f2: "a"});
db.getCollection("test").insert({f0: "A", f1: "b", f2: "B"});
db.getCollection("test").insert({f0: "A", f1: "b", f2: "b"});
db.getCollection("test").insert({f0: "A", f1: "b"});
db.getCollection("test").insert({f0: "A"});
db.getCollection("test").insert({f0: "a", f1: "A", f2: "A"});
db.getCollection("test").insert({f0: "a", f1: "A", f2: "a"});
db.getCollection("test").insert({f0: "a", f1: "A", f2: "B"});
db.getCollection("test").insert({f0: "a", f1: "A", f2: "b"});
db.getCollection("test").insert({f0: "a", f1: "A"});
db.getCollection("test").insert({f0: "a", f1: "a", f2: "A"});
db.getCollection("test").insert({f0: "a", f1: "a", f2: "a"});
db.getCollection("test").insert({f0: "a", f1: "a", f2: "B"});
db.getCollection("test").insert({f0: "a", f1: "a", f2: "b"});
db.getCollection("test").insert({f0: "a", f1: "a"});
db.getCollection("test").insert({f0: "a", f1: "B", f2: "A"});
db.getCollection("test").insert({f0: "a", f1: "B", f2: "a"});
db.getCollection("test").insert({f0: "a", f1: "B", f2: "B"});
db.getCollection("test").insert({f0: "a", f1: "B", f2: "b"});
db.getCollection("test").insert({f0: "a", f1: "B"});
db.getCollection("test").insert({f0: "a", f1: "b", f2: "A"});
db.getCollection("test").insert({f0: "a", f1: "b", f2: "a"});
db.getCollection("test").insert({f0: "a", f1: "b", f2: "B"});
db.getCollection("test").insert({f0: "a", f1: "b", f2: "b"});
db.getCollection("test").insert({f0: "a", f1: "b"});
db.getCollection("test").insert({f0: "a"});
db.getCollection("test").insert({f0: "B", f1: "A", f2: "A"});
db.getCollection("test").insert({f0: "B", f1: "A", f2: "a"});
db.getCollection("test").insert({f0: "B", f1: "A", f2: "B"});
db.getCollection("test").insert({f0: "B", f1: "A", f2: "b"});
db.getCollection("test").insert({f0: "B", f1: "A"});
db.getCollection("test").insert({f0: "B", f1: "a", f2: "A"});
db.getCollection("test").insert({f0: "B", f1: "a", f2: "a"});
db.getCollection("test").insert({f0: "B", f1: "a", f2: "B"});
db.getCollection("test").insert({f0: "B", f1: "a", f2: "b"});
db.getCollection("test").insert({f0: "B", f1: "a"});
db.getCollection("test").insert({f0: "B", f1: "B", f2: "A"});
db.getCollection("test").insert({f0: "B", f1: "B", f2: "a"});
db.getCollection("test").insert({f0: "B", f1: "B", f2: "B"});
db.getCollection("test").insert({f0: "B", f1: "B", f2: "b"});
db.getCollection("test").insert({f0: "B", f1: "B"});
db.getCollection("test").insert({f0: "B", f1: "b", f2: "A"});
db.getCollection("test").insert({f0: "B", f1: "b", f2: "a"});
db.getCollection("test").insert({f0: "B", f1: "b", f2: "B"});
db.getCollection("test").insert({f0: "B", f1: "b", f2: "b"});
db.getCollection("test").insert({f0: "B", f1: "b"});
db.getCollection("test").insert({f0: "B"});
db.getCollection("test").insert({f0: "b", f1: "A", f2: "A"});
db.getCollection("test").insert({f0: "b", f1: "A", f2: "a"});
db.getCollection("test").insert({f0: "b", f1: "A", f2: "B"});
db.getCollection("test").insert({f0: "b", f1: "A", f2: "b"});
db.getCollection("test").insert({f0: "b", f1: "A"});
db.getCollection("test").insert({f0: "b", f1: "a", f2: "A"});
db.getCollection("test").insert({f0: "b", f1: "a", f2: "a"});
db.getCollection("test").insert({f0: "b", f1: "a", f2: "B"});
db.getCollection("test").insert({f0: "b", f1: "a", f2: "b"});
db.getCollection("test").insert({f0: "b", f1: "a"});
db.getCollection("test").insert({f0: "b", f1: "B", f2: "A"});
db.getCollection("test").insert({f0: "b", f1: "B", f2: "a"});
db.getCollection("test").insert({f0: "b", f1: "B", f2: "B"});
db.getCollection("test").insert({f0: "b", f1: "B", f2: "b"});
db.getCollection("test").insert({f0: "b", f1: "B"});
db.getCollection("test").insert({f0: "b", f1: "b", f2: "A"});
db.getCollection("test").insert({f0: "b", f1: "b", f2: "a"});
db.getCollection("test").insert({f0: "b", f1: "b", f2: "B"});
db.getCollection("test").insert({f0: "b", f1: "b", f2: "b"});
db.getCollection("test").insert({f0: "b", f1: "b"});
db.getCollection("test").insert({f0: "b"});
db.getCollection("test").insert({ f1: "A", f2: "A"});
db.getCollection("test").insert({ f1: "A", f2: "a"});
db.getCollection("test").insert({ f1: "A", f2: "B"});
db.getCollection("test").insert({ f1: "A", f2: "b"});
db.getCollection("test").insert({ f1: "A"});
db.getCollection("test").insert({ f1: "a", f2: "A"});
db.getCollection("test").insert({ f1: "a", f2: "a"});
db.getCollection("test").insert({ f1: "a", f2: "B"});
db.getCollection("test").insert({ f1: "a", f2: "b"});
db.getCollection("test").insert({ f1: "a"});
db.getCollection("test").insert({ f1: "B", f2: "A"});
db.getCollection("test").insert({ f1: "B", f2: "a"});
db.getCollection("test").insert({ f1: "B", f2: "B"});
db.getCollection("test").insert({ f1: "B", f2: "b"});
db.getCollection("test").insert({ f1: "B"});
db.getCollection("test").insert({ f1: "b", f2: "A"});
db.getCollection("test").insert({ f1: "b", f2: "a"});
db.getCollection("test").insert({ f1: "b", f2: "B"});
db.getCollection("test").insert({ f1: "b", f2: "b"});
db.getCollection("test").insert({ f1: "b"});
db.getCollection("test").insert({});
db.getCollection('test').find({}).sort({"f0": 1,"f1": 1,"f2": 1});
/* 1 */
{
"_id" : ObjectId("564d88d97b9999b45267d737")
}
/* 2 */
{
"_id" : ObjectId("564d88d97b9999b45267d727"),
"f1" : "A"
}
/* 3 */
{
"_id" : ObjectId("564d88d97b9999b45267d723"),
"f1" : "A",
"f2" : "A"
}
/* 4 */
{
"_id" : ObjectId("564d88d97b9999b45267d725"),
"f1" : "A",
"f2" : "B"
}
/* 5 */
{
"_id" : ObjectId("564d88d97b9999b45267d724"),
"f1" : "A",
"f2" : "a"
}
/* 6 */
{
"_id" : ObjectId("564d88d97b9999b45267d726"),
"f1" : "A",
"f2" : "b"
}
/* 7 */
{
"_id" : ObjectId("564d88d97b9999b45267d731"),
"f1" : "B"
}
/* 8 */
{
"_id" : ObjectId("564d88d97b9999b45267d72d"),
"f1" : "B",
"f2" : "A"
}
/* 9 */
{
"_id" : ObjectId("564d88d97b9999b45267d72f"),
"f1" : "B",
"f2" : "B"
}
/* 10 */
{
"_id" : ObjectId("564d88d97b9999b45267d72e"),
"f1" : "B",
"f2" : "a"
}
/* 11 */
{
"_id" : ObjectId("564d88d97b9999b45267d730"),
"f1" : "B",
"f2" : "b"
}
/* 12 */
{
"_id" : ObjectId("564d88d97b9999b45267d72c"),
"f1" : "a"
}
/* 13 */
{
"_id" : ObjectId("564d88d97b9999b45267d728"),
"f1" : "a",
"f2" : "A"
}
/* 14 */
{
"_id" : ObjectId("564d88d97b9999b45267d72a"),
"f1" : "a",
"f2" : "B"
}
/* 15 */
{
"_id" : ObjectId("564d88d97b9999b45267d729"),
"f1" : "a",
"f2" : "a"
}
/* 16 */
{
"_id" : ObjectId("564d88d97b9999b45267d72b"),
"f1" : "a",
"f2" : "b"
}
/* 17 */
{
"_id" : ObjectId("564d88d97b9999b45267d736"),
"f1" : "b"
}
/* 18 */
{
"_id" : ObjectId("564d88d97b9999b45267d732"),
"f1" : "b",
"f2" : "A"
}
/* 19 */
{
"_id" : ObjectId("564d88d97b9999b45267d734"),
"f1" : "b",
"f2" : "B"
}
/* 20 */
{
"_id" : ObjectId("564d88d97b9999b45267d733"),
"f1" : "b",
"f2" : "a"
}
/* 21 */
{
"_id" : ObjectId("564d88d97b9999b45267d735"),
"f1" : "b",
"f2" : "b"
}
/* 22 */
{
"_id" : ObjectId("564d88d97b9999b45267d6e3"),
"f0" : "A"
}
/* 23 */
{
"_id" : ObjectId("564d88d97b9999b45267d6d3"),
"f0" : "A",
"f1" : "A"
}
/* 24 */
{
"_id" : ObjectId("564d88d97b9999b45267d6cf"),
"f0" : "A",
"f1" : "A",
"f2" : "A"
}
/* 25 */
{
"_id" : ObjectId("564d88d97b9999b45267d6d1"),
"f0" : "A",
"f1" : "A",
"f2" : "B"
}
/* 26 */
{
"_id" : ObjectId("564d88d97b9999b45267d6d0"),
"f0" : "A",
"f1" : "A",
"f2" : "a"
}
/* 27 */
{
"_id" : ObjectId("564d88d97b9999b45267d6d2"),
"f0" : "A",
"f1" : "A",
"f2" : "b"
}
/* 28 */
{
"_id" : ObjectId("564d88d97b9999b45267d6dd"),
"f0" : "A",
"f1" : "B"
}
/* 29 */
{
"_id" : ObjectId("564d88d97b9999b45267d6d9"),
"f0" : "A",
"f1" : "B",
"f2" : "A"
}
/* 30 */
{
"_id" : ObjectId("564d88d97b9999b45267d6db"),
"f0" : "A",
"f1" : "B",
"f2" : "B"
}
/* 31 */
{
"_id" : ObjectId("564d88d97b9999b45267d6da"),
"f0" : "A",
"f1" : "B",
"f2" : "a"
}
/* 32 */
{
"_id" : ObjectId("564d88d97b9999b45267d6dc"),
"f0" : "A",
"f1" : "B",
"f2" : "b"
}
/* 33 */
{
"_id" : ObjectId("564d88d97b9999b45267d6d8"),
"f0" : "A",
"f1" : "a"
}
Answer the question
In order to leave comments, you need to log in
Make a separate field under the index, for example:
{f0: "A", f1: "a", f2: "B"} -> _index: "aab"
{ f1: "A", f2: "b"} -> _index: "xab"
"x" instead of a missing field, something used to go to the end of the sort Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question