Skip to content

Commit 296a239

Browse files
authored
Move "_id" to the first column (#26)
1 parent 4cb9095 commit 296a239

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

pkg/plugin/query.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ func CreateTableFramesFromQuery(ctx context.Context, tableName string, cursor *m
101101
}
102102

103103
frame := data.NewFrame(tableName)
104-
for _, c := range columns {
105-
c.Rectify()
104+
105+
if c, ok := columns["_id"]; ok {
106106
frame.Fields = append(frame.Fields, c.Field)
107107
}
108108

109+
for _, c := range columns {
110+
if c.Name != "_id" {
111+
c.Rectify()
112+
frame.Fields = append(frame.Fields, c.Field)
113+
}
114+
}
115+
109116
return frame, nil
110117
}

pkg/plugin/query_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,4 +784,35 @@ func TestCreateTableFramesFromQuery(t *testing.T) {
784784
t.Fatal(err)
785785
}
786786
})
787+
788+
t.Run("_id is the first field if exists", func(t *testing.T) {
789+
ctx := context.Background()
790+
toInsert := []interface{}{
791+
bson.M{
792+
"_id": primitive.NewObjectID(),
793+
"_g": 1,
794+
},
795+
bson.M{
796+
"_id": primitive.NewObjectID(),
797+
"_h": 2,
798+
},
799+
bson.M{
800+
"_id": primitive.NewObjectID(),
801+
"_j": 3,
802+
},
803+
}
804+
805+
cursor, err := mongo.NewCursorFromDocuments(toInsert, nil, nil)
806+
if err != nil {
807+
t.Fatal(err)
808+
}
809+
810+
frames, err := CreateTableFramesFromQuery(ctx, "test", cursor)
811+
if err != nil {
812+
t.Fatal(err)
813+
}
814+
815+
assertEq(t, frames.Fields[0].Name, "_id")
816+
})
817+
787818
}

0 commit comments

Comments
 (0)