Commit 2c37686c authored by Chris's avatar Chris
Browse files

Basic CRUD accomplished; there are still some other functions that this didn't cover

parent 32b45e8a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@
  </component>
  <component name="ChangeListManager">
    <list default="true" id="e05ad976-0f37-4f63-bd6a-efc3a3789b88" name="Default Changelist" comment="">
      <change beforePath="$PROJECT_DIR$/Go API/First try with MongoDB/main.go" beforeDir="false" />
      <change beforePath="$PROJECT_DIR$/Go and MongoDB/First try/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/Go and MongoDB/First try/main.go" afterDir="false" />
    </list>
    <option name="SHOW_DIALOG" value="false" />
    <option name="HIGHLIGHT_CONFLICTS" value="true" />
+43 −10
Original line number Diff line number Diff line
@@ -64,17 +64,28 @@ func main() {
	// This may be useful when updating records for several students in a class, for example.
	// Note that updating a single record will replace its contents with whatever is specified, while UpdateMany() will
	//	append the docuemnt or replace a record.
	updateSingelRecord(ctx, bson.M{"title": "Inserted from Go"}, bson.M{
		"title": "Inserted from Go",
		"body": "This post was appended using the ReplaceOne function.",
	})
	// updateSingelRecord(ctx, bson.M{"title": "Inserted from Go"}, bson.M{
	// 	"title": "Inserted from Go",
	// 	"body": "This post was appended using the ReplaceOne function.",
	// })


	// // Why not, update multiple records.
	// updateMultipleRecords(ctx, bson.M{"title": "Hello!"}, bson.D{
	// 	{"$set", bson.D{{"lastUpdated", time.Now()}},
	// 	},
	// })

	// // Delete a post
	// insertPost(ctx, bson.D{
	// 	{"title", "Delete me!"},
	// 	{"body", "I am about to demonstrate how to delete a file. For that, create me first in the Posts collection."},
	// })
	// // deletePost(ctx, bson.M{"title": "Delete me!"})
	// deleteManyPosts(ctx, bson.M{"title": "Delete me!"})

	// Why not, update multiple records.
	updateMultipleRecords(ctx, bson.M{"title": "Hello!"}, bson.D{
		{"$set", bson.D{{"lastUpdated", time.Now()}},
		},
	})
	// // Drop a collection and all of its associated documents
	// dropCollection(ctx, postsCollection)
}

func insertPost(ctx context.Context, newPost bson.D) {
@@ -83,7 +94,7 @@ func insertPost(ctx context.Context, newPost bson.D) {
		log.Fatal(err)
	}

	fmt.Printf("Record with ID %v inserted into the collection!", insertResult.InsertedID)
	fmt.Printf("Record with ID %v inserted into the collection!\n", insertResult.InsertedID)
}

func getAllPosts(ctx context.Context, batches bool) {
@@ -145,3 +156,25 @@ func updateMultipleRecords(ctx context.Context, filter bson.M, appendObject bson
	}
	fmt.Printf("Updated %v document(s)\n", result.ModifiedCount)
}

func deletePost(ctx context.Context, filter bson.M) {
	result, err := postsCollection.DeleteOne(ctx, filter)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("deletePost removed %v document(s)\n", result.DeletedCount)
}

func deleteManyPosts(ctx context.Context, filter bson.M) {
	result, err := postsCollection.DeleteMany(ctx, filter)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("deleteManyPosts removed %v document(s)\n", result.DeletedCount)
}

func dropCollection(ctx context.Context, collectionToDrop mongo.Collection) {
	if err := collectionToDrop.Drop(ctx); err != nil {
		log.Fatal(err)
	}
}
 No newline at end of file