1
votes

I have a tagging system in my mind and here are my ideas,

1, I can make a mongodb database and add tags to an embedded field and index them all

2, I can use SQL for it, I can add the tags as a new entry to a table

3, I can create another collection on mongodb and do it like this

for example the tag is "chicken", this is the document

tag:"chicken",
id:"1200000,32555,8787888888"

the tag is the chicken, the id is other documents that relate with the chicken, I think those systems will give me lots of headaches. especially the first one. Do you have any tagging system ideas for my need ?, by the way, there will be more than one million different tags and searched this tagging system before opening this question,

thank you

2

2 Answers

1
votes

If you're trying to achieve this in mongo, i would create documents that look like

{
    tag: "chicken",
    ids : [ 12345,23456,34567 ]
},
...

then two indexes, one on "tag" and a multikey index on "ids" (http://docs.mongodb.org/manual/core/indexes/#multikey-indexes).

db.tags.ensureIndex( { "tag": 1 } );
db.tags.ensureIndex( { "ids": 1 } );

depending on what queries you need to do there may be value in creating a compound index on both attributes. http://docs.mongodb.org/manual/core/indexes/#compound-indexes

e.g.

db.tags.ensureIndex( { "ids": 1, "tag" : 1 } );

1 million tag docs will not be an issue, I have similar collection with ~3m docs each having on avg 15 items in the multikey index and performance is excellent (from dev vm).

i hope this helps

1
votes

MongoDB is great for such tagging thanks to how it indexes lists. Consider a document like this one:

{_id: 42, name: "someName", tags: ["chicken", "parrot", "hovercraft"]}

If you add an index for tags field, you can efficiently perform queries like this:

{tags: "chicken"}

This query will find all documents that contain "chicken" in its tags list - that is, all documents tagged with chicken tag.

I'm not sure if this is what you meant in your first point. If it is then I don't understand why it gives you headaches.