Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to create document inside collection in MongoDB

Tags:

php

mongodb

nosql

I'm using PHP and MySQL for social net work system

I have MySQL table named member_feed , in this table I save feeds for each member, my table structure is :

    |member_feed_id | member_id | content_id | activity_id |

in this table I have more than 120 million record, and every member has set of record.

I currently work to migrate from MySQL to MongoDB and I'm new on MongoDB. so I need to convert this table to collection in MongoDB. I think to build my collection for member_feed table like :

1- each row from member_feed table as document , like:

{  "member_id: 1" , "content_id: 10" , "activity_id: 1" },
{  "member_id: 1" , "content_id: 11" , "activity_id: 2" },
{  "member_id: 1" , "content_id: 12" , "activity_id: 3" },
{  "member_id: 2" , "content_id: 9" ,  "activity_id: 4" },
{  "member_id: 2" , "content_id: 11" , "activity_id: 5" },
{  "member_id: 2" , "content_id: 14" , "activity_id: 6" }

so this collection will have 120 million record

2- all member feed for one document that have member id and all feeds for this member

{
        member_id: '1',
        feeds: [
            { content_id: '10', 'activity_id' : 1 },
            { content_id: '11', 'activity_id' : 2 },
            { content_id: '12', 'activity_id' : 3 }
        ]

        member_id: '2',
        feeds: [
            { content_id: '9',  'activity_id' : 4 },
            { content_id: '11', 'activity_id' : 2 },
            { content_id: '14', 'activity_id' : 6 }
        ]
    }

so when I need to view user feed on his dashboard I will do :

1-get all ids from document inside mongoDB as $contentIdsFromMongo by member_id

2- run query in MySQL to get the content like

select * from content where content_id in($contentIdsFromMongo); 

and $contentIdsFromMongo my be have 10000 ids , so is this affect to MySQL performance.

and I have mongoDB on server and MySQL on another server

and in every second the system insert a lot of record for each member, so I need the document to be able to do all operation like (insert, update, select,..)

what is the best way to create the documents in collection for each member, and is this way right or wrong for working with mongoDB

Thanks.

like image 819
Osama Jetawe Avatar asked Aug 30 '13 13:08

Osama Jetawe


People also ask

How to create a new collection in MongoDB?

In MongoDB, a new collection is created when we add one or more documents to it. We can insert documents in the collection using the following methods: Here, we create a collection named as myNewCollection1 by inserting a document that contains a name field with its value in it using insertOne () method.

How do I add documents to a MongoDB database?

MongoDB provides the insert () method (and two others) for adding documents to a database. MongoDB provides the following three methods for inserting documents into a database: The insert () method inserts one or more documents into a collection. Each document is provided as a parameter. The collection name is prepended to the insert () method.

What are the features of MongoDB?

As MongoDB is a Schema-free database, it can store the documents that are not the same in structure. Also, there is no need to define the columns and their datatype. Database: The MongoDB database is a container for collections and it can store one or more collections. It is not necessary to create a database before you work on it.

What is the difference between MongoDB and a database?

MongoDB stores data records as documents (specifically BSON documents) which are gathered together in collections. A database stores one or more collections of documents. In MongoDB, databases hold one or more collections of documents. To select a database to use, in mongosh, issue the use <db> statement, as in the following example:


1 Answers

Two main considerations in data modelling for document stores:

  1. how data will grow and change over time, and
  2. the kinds of queries your application will perform.

You have provided info around how data is expected to grow. Just on that basis, I would suggest option #1.

{  "member_id: 1" , "content_id: 10" , "activity_id: 1" }

But, it is also important to understand what kind of queries you will be performing and how you intend to use this data in your application.

Additional options possible are:

  • one document per member with an array of feeds in each
  • grouping documents by content or activity id, if that is how your application would query the data.

So in summary, to come up with an optimal model, answering question 2 above is also important.

PS: Option 2 in your question definitely sounds like a bad idea given how you have described data will grow.

You may also refer to this link for some additional guidelines: http://docs.mongodb.org/manual/core/data-modeling/

like image 70
Anurag Kapur Avatar answered Nov 15 '22 08:11

Anurag Kapur