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.
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.
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.
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.
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:
Two main considerations in data modelling for document stores:
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:
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With