1

In my nodejs/mongo/mongoose application the users are associated with different codes. these are 3 letter codes, like ABC, XYZ, ACD, etc. each person can have zero to max 20 codes like that. a sample data would look like this:

[
{name: John, codes: 'abc, bbb, dyd, zzz'},
{name: Sammy, codes: 'haz, rty, dyd'},
{name: Suzanne, codes: 'abc, ccc, mnm, zzz'}
]

when i search for people, i search for those codes. for example: find me all the people with code 'abc' (and have other properties).

The question: how to store the users codes data for best performance? in a simple string like in my example? different rows with id's and index? and how to query for people with a specific code in mongoose?

there can be millions of people, and not that many codes, maybe 30 altogether, and each person will have between 0 and 10 or 20 codes.

Amir Gilboa
  • 128
  • 8

2 Answers2

0

I think storing them in a string is opening you up to making mistakes and makes it difficult to change codes in the future. In my opinion, you're best off using an array to store these codes and giving each of the codes a 1-30 identifier. Then you can push the identifier for the appropriate code into their code array. As for querying, take a look at this question.

Community
  • 1
  • 1
Josh Slate
  • 774
  • 5
  • 17
0

put the codes in a list as mentioned already like codes: ["abc", "bbb", "dyd", "zzz"]; there are plethora of ways to addedit. you please check mongodb site. you can always retrieve by simply db.collection.find({codes:"abc"}) or by
db.collection.find( { $and:[{codes:"abc"}, {codes:"bbb"}] }) for multiple code search for the same person. An ex.

`

db.array.insert({_id:0,codes:[]})--- WriteResult({ "nInserted" : 1 })--- db.array.find()--- { "_id" : 0, "codes" : [ ] }--- db.array.update({_id:0 },{$push:{"codes":"aaa"}} );--- WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })--- db.array.find()--- { "_id" : 0, "codes" : [ "aaa" ] }`

amitava
  • 505
  • 1
  • 5
  • 10
  • looks good. what's the data type for the codes? array? so adding values to it would simply be myArray.push('abc'); ? – Amir Gilboa May 06 '16 at 01:18