官术网_书友最值得收藏!

Writing and reading test data

Follow these steps to write and read test data:

  1. First, in the mongo shell for the ReplicaSet primary node (as a prompt, you will see replSet0:PRIMARY> ), let's add 1,000 sample documents in the demo collection:
for (var i = 1; i <= 1000; i++) {
db.demo.insert( { exampleValue : i } )
}
  1. You can quickly verify the inserted documents by using the find() method on the demo collection:
db.demo.find()
  1. Now, we will create a minimalistic .NET Core 3.0 console application running in a Docker container. This will connect to the ReplicaSet running in our Docker container, query our demo collection, and write a value of exampleValue for each document to standard output.

You can find the source code and Dockerfile for this in this book's GitHub repository: https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/tree/master/Chapter02/04_MongoDB_dotnet.

If, during the execution of this scenario, you experience any instability issues with MongoDB, consider upgrading the mongo-1903 Dockerfile to the latest MongoDB version.

To read our test data, we need to build the application Docker image and create a container that's running in the mongo-cluster network. Perform the following steps to do so:

  1. Clone the repository and navigate to the Chapter02/04_MongoDB_dotnet directory in PowerShell.
  2. Execute docker build in the current directory in order to create the mongo-dotnet-sample Docker image:
docker build -t mongo-dotnet-sample:latest .
  1. Run the sample container. This needs to be connected to the mongo-cluster network:
docker run --isolation=process `
--rm `
--net mongo-cluster `
mongo-dotnet-sample:latest

In the output, you should see an increasing sequence of numbers, which is the values of exampleValue in our test documents:

If you are curious, you can check what the SMB share contains on Azure Portal (https://portal.azure.com/):

Congratulations! You have successfully created and tested a MongoDB ReplicaSet running in a Windows container with Azure Files SMB share being used as a bind mount for storing data. Let's quickly summarize what we have learned in this chapter.

主站蜘蛛池模板: 龙岩市| 包头市| 民勤县| 广西| 日照市| 东安县| 股票| 平乡县| 孝义市| 石景山区| 大姚县| 凤冈县| 张家界市| 诸暨市| 西乌珠穆沁旗| 南宁市| 江永县| 惠州市| 鲁山县| 富裕县| 海原县| 淮滨县| 永善县| 达孜县| 彰化市| 如皋市| 阳春市| 大化| 图们市| 忻城县| 大荔县| 万州区| 阿拉善盟| 郯城县| 安宁市| 曲周县| 剑川县| 马公市| 西丰县| 新田县| 南通市|