Skip to content

3. Insert sample data

In this tutorial you will learn to insert sample data to Percona Server for MongoDB.

MongoDB provides multiple methods for data insert . We will use a For loop to insert some sample documents.

  1. Run the following command:

    admin> for (var i = 1; i <= 50; i++) {
       db.test.insertOne( { x : i } )
    }
    

    If there is no test collection created, MongoDB creates when inserting documents.

    Output
    {
      acknowledged: true,
      insertedId: ObjectId("652567e5eedca48f97e1868f")
    }
    
  2. Query the collection to verify the data insertion

    admin> db.test.find()
    
    Output
    [
      { _id: ObjectId("652567e4eedca48f97e1865e"), x: 1 },
      { _id: ObjectId("652567e4eedca48f97e1865f"), x: 2 },
      { _id: ObjectId("652567e4eedca48f97e18660"), x: 3 },
      { _id: ObjectId("652567e4eedca48f97e18661"), x: 4 },
      { _id: ObjectId("652567e4eedca48f97e18662"), x: 5 },
      { _id: ObjectId("652567e4eedca48f97e18663"), x: 6 },
      { _id: ObjectId("652567e4eedca48f97e18664"), x: 7 },
      { _id: ObjectId("652567e4eedca48f97e18665"), x: 8 },
      { _id: ObjectId("652567e4eedca48f97e18666"), x: 9 },
      { _id: ObjectId("652567e4eedca48f97e18667"), x: 10 },
      { _id: ObjectId("652567e4eedca48f97e18668"), x: 11 },
      { _id: ObjectId("652567e4eedca48f97e18669"), x: 12 },
      { _id: ObjectId("652567e4eedca48f97e1866a"), x: 13 },
      { _id: ObjectId("652567e4eedca48f97e1866b"), x: 14 },
      { _id: ObjectId("652567e4eedca48f97e1866c"), x: 15 },
      { _id: ObjectId("652567e4eedca48f97e1866d"), x: 16 },
      { _id: ObjectId("652567e4eedca48f97e1866e"), x: 17 },
      { _id: ObjectId("652567e4eedca48f97e1866f"), x: 18 },
      { _id: ObjectId("652567e4eedca48f97e18670"), x: 19 },
      { _id: ObjectId("652567e4eedca48f97e18671"), x: 20 }
    ]
    

    You will have different _id values.

Now your cluster has some data in it.

Next steps

Make a backup

Get expert help

If you need assistance, visit the community forum for comprehensive and free database knowledge, or contact our Percona Database Experts for professional support and services. Join K8S Squad to benefit from early access to features and “ask me anything” sessions with the Experts.


Last update: 2024-06-07