0

Say I need every users name often. Instead of querying for every document and then plucking this attribute what is the best way to keep an index I can quickly call?

Should I add another document that is just an array of these attributes and then manually edit it every time I add or remove a user? Is there a better way using mongo indexing or something?

Thanks for any guidance.

boom
  • 10,856
  • 9
  • 43
  • 64
  • Are you trying to store info for just the currently logged in user? – Farzher Oct 28 '12 at 04:17
  • This is particular info I need on all of my users, not just logged in. – boom Oct 28 '12 at 04:19
  • Isn't that what the database is for? xD I don't think I get it. Put an index on the name attribute? `db.users.ensureIndex({name:1});` – Farzher Oct 28 '12 at 04:22
  • @StephenSarcsamKamenar how do i query that and get an array I can use?? – boom Oct 28 '12 at 04:37
  • do that index above and do the query: `db.users.find({}, {fields: {name: 1}}).toArray(function(err, users) {callback(null, users.map(function(user) {return user.name}))})` – Jonathan Ong Nov 19 '12 at 04:58

2 Answers2

1

You may want to set up some sort of caché. I'd use a redis machine for that. That way you could avoid to go to MongoDB so often.

Herman Junge
  • 2,759
  • 24
  • 20
  • 1
    I ended up doing this pretty much, storing it in memory at the moment. I query mongo once on startup and then add remove from the index in my add/remove mongo methods. – boom Oct 28 '12 at 15:44
-1

You don't need to:

  • Change the way you query
  • Change your schema

MongoDB, and pretty much any other database, has something called indexes. These allow for O(log n) operations on data values which result in much faster queries, not only that but they are designed to be a sort of cache to your data and can reside in memory.

You can simply imply an index on name by:

db.uses.ensureIndex({name:1})

I would recommend you look into this side of databases more:

Those links should get you started. The theory and implementation of indexes is mostly the same across all databases. MongoDB does do some fun stuff with memory but apart from that it is the same.

Edit

I realised I read the question a little wrong. You want to store a list of ALL users names since you need them all at once.

For a large user base I don't think Redis could do this adequately (actually I am 90% sure it can't) and nor could any aggregated document structure in MongoDB and housing that for a large user based in memory would be troublesome to say the least, one for memory requirements of housing the structure.

I recommend you really think of why you need that. You should really plan out your query scenario and think of the requirements, you are going down an odd path here.

Community
  • 1
  • 1
Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • ok, i can index by the value sure. Then how do i query the db to get a list of all names? it doesn't matter if it's indexed i still need to get a pluck every doc. – boom Oct 28 '12 at 15:42
  • Downvoter care to explain? Anyway to @boom What do you mean pluck every doc? You have to do the same thing with redis and with an array of docs client side, how many users are we talking about, this would never scale for 100K+ users. – Sammaye Oct 28 '12 at 16:10
  • I'm not trying to index values so I can retrieve them faster. I need an array of all of a particular value from every document. I'm thinking I will just updated an array of these values saved in mongo when I add/remove/change any document, but i wanted to see if mongo had any way of inherently doing this for a collection. – boom Oct 31 '12 at 03:04
  • @boom I suppose it depends on the size of the data. You could run the query you do to get all usernames and then store that within a single row in MongoDB as you stated, this is the only way in MongoDB. However I would like to say this still sounds very odd and I got a feeling that the data could expand to the point where you will realise you will have to completely rethink this. – Sammaye Oct 31 '12 at 11:24