Muhammad Saquib Shaikh
Muhammad Saquib Shaikh

Reputation: 298

What will be best method to store this type of data in mongoDb?

I have to store this type of data in mongodb , for particular user there will be n number of customers whose details have to store. What will be best strategy to store. should I create collection for each user and store customer details in that collection. Suggest method to store and easy retreival.

{
  "user1": {
    "customer1": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    },
    "customer2": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    },
     "customer3": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    }
  },
   "user2": {
    "customer1": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    },
    "customer2": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    },
     "customer3": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    }
  },
  "user3": {
    "customer1": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    },
    "customer2": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    },
     "customer3": {
      "name": "name of customer",
      "ageGroup": "20-30",
      "localtion": "customer location",
      "email": "[email protected]",
      "contact": "91991199199",
      "country": "anywhere"
    }
  }
}

Upvotes: 0

Views: 41

Answers (1)

Towerss
Towerss

Reputation: 674

I would suggest using the Attribute pattern as you have many similar fields within your objects.

The structure could look like:

{
  "user1": {
    "customers": [
      {
        "id": "customer1",
        "name": "name of customer",
        "ageGroup": "20-30",
        "localtion": "customer location",
        "email": "[email protected]",
        "contact": "91991199199",
        "country": "anywhere"
      },
      {
        "id": "customer2",
        "name": "name of customer",
        "ageGroup": "20-30",
        "localtion": "customer location",
        "email": "[email protected]",
        "contact": "91991199199",
        "country": "anywhere"
      }
    ]
  },
  "user2": {
    "customers": [
      {
        "id": "customer1",
        "name": "name of customer",
        "ageGroup": "20-30",
        "localtion": "customer location",
        "email": "[email protected]",
        "contact": "91991199199",
        "country": "anywhere"
      },
      {
        "id": "customer2",
        "name": "name of customer",
        "ageGroup": "20-30",
        "localtion": "customer location",
        "email": "[email protected]",
        "contact": "91991199199",
        "country": "anywhere"
      }
    ]
  }
}

This would allow you to create fewer search indexes for the array of objects like:

{ "customers.id": 1, "customers.email": 1}

Upvotes: 1

Related Questions