I'm trying to implement a time series data in a mongo db where each document can only have x number or fill records(time and fill). I want it so if it tries to update and there are to many records it grabs the information from attempted update not included in query and then inserts that as a new record
Ive tried using update with upsert and findOneandUpdate but I'm assuming beucse I have samples: {$lt: MaxSamples} it just finds nothing
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
", 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)
it just inserts a record with query parameters