我正在尝试在mongo db中实现时间序列数据,其中每个文档只能具有x号或填充记录(时间和填充)。我想要这样,如果它尝试更新并且有很多记录,它会从尝试更新中获取未包含在查询中的信息,然后将其作为新记录插入
我尝试将upsert和findOneandUpdate用于更新,但我假设是因为我有示例:{$ lt:MaxSamples}它什么也没发现
var b doppl_server.Bin
testLoc := doppl_server.Location{Long: 44.231, Lat: 44.231}
testBin := doppl_server.Bin{
ID: primitive.NewObjectID(),
NodeID: "cb250a2d8a5aeece",
NumberSamples: 1,
Loc: testLoc,
Org: "doppl",
Type: 1,
Date: ISODate(2019, time.July, 11),
Fill: []doppl_server.Fill{{33.12452346, uint32(time.Now().Unix())}},
Serviced: []doppl_server.Service{},
}
conn, _ := connect()
collection := conn.Database("doppl").Collection("bins")
rest, err := collection.InsertOne(context.TODO(), testBin)
if err != nil {
log.Fatal(err)
}
fmt.Println(rest)
err = collection.FindOne(context.TODO(), bson.D{{"date", ISODate(2019, time.July, 11)}}).Decode(&b)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", b)
sample := doppl_server.Fill{Fill: 44.5454, Time: uint32(time.Now().Unix())}
result:= collection.FindOneAndUpdate(context.TODO(), bson.M{"nodeid": "cb250a2d8a5aeece", "nsamples": bson.M{"$lt": MaxSample}, "date": ISODate(2019, time.July, 12)}, bson.M{"$push": bson.M{"fill": sample}, "$inc": bson.M{"nsamples": 1}},options.FindOneAndUpdate().SetUpsert(true))
fmt.Println(result)
它只是插入带有查询参数的记录