var result = _context.Messages
.Where(x => x.timestamp != null)
.GroupBy(x => x.timestamp.Value.Year.ToString()+"-" + x.timestamp.Value.Month.ToString())
.Select(x => new { date = x.Key, count = x.Count() })
答案 0 :(得分:0)
要在go lang MongoDB驱动程序中运行 $ facet ,请使用以下代码,只需替换您的连接字符串和数据库名称
package main
import (
"context"
"time"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb_connect_string"))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
database := client.Database("testdb")
transFront := database.Collection("transFront")
query1 := bson.A{bson.D{{"$match", bson.D{{"appName", "app-38"}}}}, bson.D{{"$count", "count"}}}
query2 := bson.A{bson.D{{"$match", bson.D{{"appName", "app-43"}}}}, bson.D{{"$count", "count"}}}
query3 := bson.A{bson.D{{"$match", bson.D{{"appName", "app--1"}}}}, bson.D{{"$count", "count"}}}
facetStage := bson.D{{"$facet", bson.D{{"query1", query1}, {"query2", query2}, {"query3", query3}}}}
showInfoCursor, err := transFront.Aggregate(ctx, mongo.Pipeline{facetStage})
if err != nil {
panic(err)
}
var showsWithInfo []bson.M
if err = showInfoCursor.All(ctx, &showsWithInfo); err != nil {
panic(err)
}
fmt.Println(showsWithInfo)
}