Created
August 30, 2019 15:30
-
-
Save skriptble/f5a5482ad4b8c69d4f75f584e0189e87 to your computer and use it in GitHub Desktop.
$push example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"go.mongodb.org/mongo-driver/bson" | |
"go.mongodb.org/mongo-driver/mongo" | |
) | |
func main() { | |
client, err := mongo.Connect(context.Background()) | |
if err != nil { | |
panic(err) | |
} | |
coll := client.Database("foo").Collection("bar") | |
coll.Drop(context.Background()) | |
_, err = coll.InsertOne(context.Background(), bson.D{ | |
{"doc_id", "qwe123"}, | |
{"embedded_doc", bson.A{ | |
bson.D{ | |
{"embedded_doc_ID", "abc123"}, | |
{"item", 1}, | |
{"data", "xyz"}, | |
{"embedded_doc_two", bson.A{ | |
bson.D{ | |
{"createdOn", "sssss"}, | |
}, | |
}}, | |
}, | |
bson.D{ | |
{"embedded_doc_ID", "xyz456"}, | |
{"item", 1}, | |
{"data", "xyz"}, | |
{"embedded_doc_two", bson.A{ | |
bson.D{ | |
{"createdOn", "zzzzz"}, | |
}, | |
}}, | |
}, | |
}}, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(coll.FindOne(context.Background(), bson.D{{"doc_id", "qwe123"}}).DecodeBytes()) | |
_, err = coll.UpdateOne(context.Background(), bson.D{{"doc_id", "qwe123"}}, bson.D{{"$push", bson.D{{"embedded_doc.1.embedded_doc_two", bson.D{{"createdOn", "aaaaa"}}}}}}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(coll.FindOne(context.Background(), bson.D{{"doc_id", "qwe123"}}).DecodeBytes()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment