我写了一些基本代码,目的是使用mongodb(官方)驱动程序创建一个存储支持的计数器集合。代码存在一些问题。
我在这里发布这个用例,希望得到一个暗示导致问题的原因,以及是否有一个更优雅/简洁的方式来解决package main
import (
"fmt"
"log"
"context"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
)
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func main() {
monday := map[string]int64{
"Cumulus": 1, "Stratus": 2, "Stratocumulus": 4, "Altocumulus": 1, "Nimbostratus": 3,
"Altostratus": 2, "Cirrocumulus": 2, "Cirrostratus": 1, "Cirrus": 1, "Cumulonimbus": 0,
}
sunday := map[string]int64{
"Cumulus": 0, "Stratus": 1, "Stratocumulus": 3, "Altocumulus": 2, "Nimbostratus": 2,
"Altostratus": 1, "Cirrocumulus": 3, "Cirrostratus": 2, "Cirrus": 0, "Cumulonimbus": 2,
}
days := []map[string]int64{sunday, monday}
client, err := mongo.Connect(context.Background(), "mongodb://localhost:27017", nil)
check(err)
db := client.Database("weather")
coll := db.Collection("clouds")
for _, today := range days {
clouds := today
for name, count := range clouds {
v := bson.NewDocument()
if err := coll.FindOne(
context.Background(),
bson.NewDocument(
bson.EC.String("name", name),
),
).Decode(v); err == mongo.ErrNoDocuments {
_, err := coll.InsertOne(
context.Background(),
bson.NewDocument(
bson.EC.String("name", name),
bson.EC.Int64("qty", count),
),
)
check(err)
} else {
prev := v.Lookup("qty").Int64()
_, err := coll.UpdateOne(
context.Background(),
bson.NewDocument(
bson.EC.String("item", name),
),
bson.NewDocument(
bson.EC.SubDocumentFromElements("$inc",
bson.EC.Int64("qty", prev + count),
),
),
)
check(err)
}
}
}
cursor, err := coll.Find(
context.Background(),
bson.NewDocument(),
)
doc := bson.NewDocument()
for cursor.Next(context.Background()) {
doc.Reset()
err := cursor.Decode(doc)
check(err)
name := doc.Lookup("name").StringValue()
qty := doc.Lookup("qty").Int64()
fmt.Println(name, qty)
}
&当密钥不存在时for _, today := range days {
clouds := today
for name, count := range clouds {
var opts []option.UpdateOptioner
opts = append(opts, mongo.Opt.Upsert(true))
filter := bson.NewDocument(bson.EC.String("name", name))
update := bson.NewDocument(
bson.EC.SubDocumentFromElements("$inc", bson.EC.Int64("qty", count)),
bson.EC.SubDocumentFromElements("$set", bson.EC.String("name", name)))
_, err := coll.UpdateOne(context.Background(), filter, update, opts...)
check(err)
}
}
。
var togglemenu = (function () {
var togSubtopics = document.getElementById("subtopics");
togSubtopics.addEventListener("click", function () {
togSubtopics.classList.toggle("show");
});
return {
togglemenu: togglemenu()
};
})();
}
更新
其他帖子并未完全解决这个问题。以下是我提出的解决此问题的代码段。我在这里发布它的目的是帮助那些遇到完全相同问题的人:
body {
margin: 0;
}
li, a{
text-decoration: none;
list-style-type: none;
text-decoration-line: none;
color: black;
}
/*main-menu*/
#mainmenu {
position: relative;
}
#mainmenu ul {
margin: 0;
padding: 0;
}
#mainmenu li {
display: inline-block;
}
#mainmenu a {
display: block;
width: 100px;
padding: 10px;
border: 1px solid;
text-align: center;
}
/*subtopics*/
#subtopics {
position: absolute;
display: none;
margin-top: 10px;
width: 100%;
left: 0;
}
.show {
display: block;
}
#subtopics ul {
margin: 0;
padding: 0;
}
#subtopics li {
display: block;
}
#subTopics a {
text-align: left;
}
/*columns*/
#column1, #column2, #column3 {
position: relative;
float: left;
left: 125px;
margin: 0px 5px 0px 0px;
}
/*hover underline*/
#mainmenu li:hover {
text-decoration: underline;
}