How to bind data in MongoDB?

I save the row in the database along with the row owners (one or more owners per row).

I have always worked with MySQL, which is a regular relational database. In this case, I would save the row with a unique identifier in one table, and then the unique identifier of the row with the owners (as several records) in the second table.

Then I could get the rows by the owners using SQL Join.

Now I am working on a project using MongoDB, and I am doing the same thing as above.

Will this be considered incorrect when working with NoSQL databases? Should I not think about “relationships” when working with NoSQL?

Another way I can think of achieving the same in MongoDB is to save it like this:

{ "string": "foobar", "owners": [ "owner1", "owner2", "owner3" ] } 

However, in this case, I’m not sure how I will look for "all lines belonging to owner1".

+6
source share
4 answers

It looks like the right approach; remember, however, it always depends on the totality of your project, what are the goals (productivity, flexibility), what queries do you intend to launch most intensively, if you need to run special queries and other factors. In the general case, the use of attached documents, as you wrote, is the right alternative to using unions and foreign keys.

Keep in mind also the maximum document size (currently 16 MB), which would be a problem if there were many (for example, hundreds of thousands) owners of this line.

+2
source

Will this be considered incorrect when working with NoSQL databases? Should I not think about “relationships” when working with NoSQL?

There are so many questions about the investment case, and it comes down to very little.

Signs that were not mentioned here that need to be considered if you want to insert:

  • Will the size of the document increase in bulk? If so, the document can often move around the disk, this is bad.
  • Will the linked string contain many references to the collection I'm working on (i.e. video cannot insert user ). If so, you may have problems copying redundant data from the corresponding line to the subdocument, especially when updating redundant data.
  • How do I display these results?

Displaying results is always a key decider in whether to embed. If you need to split pages into a large number of lines, say 1000, you will need to use the $slice operator in a regular query or in an aggregation structure. At 1000, I admit that it can be pretty fast, but sooner or later this memory operation will be slower than a regular procedure (it should always be infact).

If you need complex sorting and display of subdocuments, you may want to separate them and create a document structure instead:

 { "string": "foobar", "owners": [ ObjectId(), ObjectId(), ObjectId() ] } 

I think it could be a more realistic structure for your data anyway, since owner sounds like a user string in the users collection.

Instead of filling out subdocuments, possibly changing user data, you can simply reference their _id . This is pretty kool, because you can embed relationships, but at the same time, the document will grow very little, which, I hope, means that there is a small chance that the disk will constantly move, not only this, but a smaller working set, which will create more efficient overall work. Not only this, but, of course, the owner’s _id rarely changes, so the only operations that you need to most likely throw at this subset of data are creating and deleting.

Return to complex sorting and pagination. With this data, you can, of course, get all the owner tags with one round trip, and then another round trip, you can query these owners for rows in the users table with a regular query using $in , which allows you to have a complex display .

So, this structure as a whole, I found, is very effective.

Of course, this structure depends on your request, it would be better to place the row identifier for the user instead, but in this case it is not, since the user can presumably own many lines, I would say that it is a lot → many links built-in to a string.

Hope this helps, and I didn’t go round,

+6
source

In addition to dbaseman's answer:

Yes, your approach looks fine. You can easily find "all lines owned by owner1"

 db.collection.find({owners: 'author1'}) 

This is possible because mongodb processes arrays in a special way.

+5
source

When working with embedded data, I would recommend that you familiarize yourself with the Atomity behavior in Mongo. A good starting point would be here: http://docs.mongodb.org/manual/core/data-modeling/#atomicity

In your particular case, when adding / removing an ObjectId object (as recommended by Sammaye) to your "owner" array, you will want to use the findAndModify () operation in the doc file to ensure that when many records in this document occur, data integrity is maintained.

As part of this operation, I would recommend using the following operators:

  • When adding owner $ addToSet to prevent duplication
  • When removing the owner of $ pull

Both documents are described here: http://docs.mongodb.org/manual/reference/operators/#update-operators-array

+3
source

Source: https://habr.com/ru/post/918856/


All Articles