0

suppose I have a database in mongodb called "test" inside this db "test", I have 3 collections, which is "test-subone", "test-subtwo", "test-subthree". let's assume that those 3 collections have an attribute in common, which is "testattr". And what I want is create a text index for attribute "testattr" in those 3 collections.

am I right if I do this way :

db.collection.createIndex({"testattr":"text"})

or do I have to create the text index for each collection, i.e. :

db.test-subone.createIndex({"testattr":"text"})
db.test-subtwo.createIndex({"testattr":"text"})
db.test-subthree.createIndex({"testattr":"text"})

and I want to ask, what is the difference between the "db.collection.createIndex" and "db..createIndex"?

Thank you very much for your help

xemjas
  • 115
  • 4
  • 16
  • 1
    yes, you have to create text index on each collection. – Saleem Mar 22 '18 at 12:08
  • oh, okay in that case, what does "db.collection.createIndex" do ? – xemjas Mar 22 '18 at 12:14
  • I'm not sure. Where did you got `db.collection.createIndex`? I believe `collection` refers to any collection – Saleem Mar 22 '18 at 12:17
  • well, it's from the mongodb's manual guide site, i.e. https://docs.mongodb.com/manual/indexes/ and I tried it on my mongodb, and it works, i.e. : db.collection.getIndexes() -> display the index that I just made. However, I'm not sure if that index is for all collection or for something else – xemjas Mar 22 '18 at 12:21
  • no, index will not cover all collections. It's just a convention to refer a collection under knife. – Saleem Mar 22 '18 at 12:23
  • oh, okay. then I will just have to create index for all collection, which is quite in pain ^^; . Thank you very much – xemjas Mar 22 '18 at 12:25

0 Answers0