I am creating a tag based search engine for various kind of things in mongodb.
I have blogs document, testimonials document, comments documents, books document and images document and all these have array of tags field.
Now when I fetch a book, which have certain tags associated with it, I would like to also fetch blogs and testimonials and comments with those tags.
I would like to the same when I fetch a blog .. fetch rest with tags that blog have.
I am designing my database model. what is the best way to handle these kind of tag based search.
currently what I am thinking is
is this the best way ? how should I design model?
Update : I will perform search more frequently.
A tag set list is an array of tag sets, where each tag set contains one or more tag/value pairs. To find replica set members, MongoDB tries each document in succession until a match is found. See Order of Tag Matching for details.
The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype: { field: { $in: [<value1>, <value2>, ... < valueN> ] } } For comparison of different BSON type values, see the specified BSON comparison order.
The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.
MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents. Documents consist of key-value pairs which are the basic unit of data in MongoDB.
If you need to repeat tags in multiple collections, I would rather do a tags collection itself.
Think if you need to change the name of one tag in the future, maybe because of a mistake like a typo, you'll need to iterate over all your collections searching for this tag to fix it. Wouldn't it be easier if you only need to replace in one place?
Embed arrays and objects in one document is a powerful tool, but there are times when it's not the best solution. This case is one of them, and you should prevent as much as you can repetition.
Official documentation talking about avoid repetition.
Create a tags collection and add their ObjectId to the tags array in the other documents instead of the tag itself. Like below.
// tags collection
{
_id: <ObjectId1>
title: "trending"
}
// all other documents (blogs, testimonials...)
{
_id: <ObjectId2>
tags: [
<ObjectId1>
],
// other stuff...
}
When you fetch one document you can get all its tags and look for other documents with related tags using the operator $in, like this:
db.blogs.find({
tags: {
$in: [
<ObjectId1>,
<ObjectIdX>,
// other tags ids
]
}
})
And this will return at once all the documents matching one or more tags.
More about $in operator.
Well used indexes have a great impact on performance. This isn't the place to teach about how they works, but mongodb have multikey indexex and in your concrete case is obviuos which one, tags.
Example:
db.blogs.createIndex( { "tags": 1 } )
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