0

I'm working with NodeJS and MongoDB,

Actually I'm have a doubt about the datastructure that I'm using in MongoDB, currently I save my data in the database like this:

{
    "_id" : ObjectId("4f9519d6684c8b1c9e72e367"),
    "tipo" : "First Post on MongoDB",
    "rates": {
            "user": "5c981a0f8a76d426dc04619e",
            "votation": 1
    },
}

But the way that I want to store the data is in this structure:

{
    "_id" : ObjectId("4f9519d6684c8b1c9e72e367"),
    "tipo" : "First Post on MongoDB",
    "rates": {
            "5c981a0f8a76d426dc04619e":{
                "votation": 1
           }
    },
}

I had been trying to save the data like in the last example, but I don't achieve it,

Here is the way as I have my const in NodeJS.

  const post = {
    tipo: body.tipo,
    user: body.usuario,
    duration: 25,
    rates: 
      {
        user: body.usuario,
        votation: 1
      }

  };

And Here is how I have my model:

interface IPost extends Document {
  tipo: String;
  user: Object;
  duration: number;
  rates: Object;
}

Can someone explain me how can I do it please?

Regards.

Machiaveli
  • 27
  • 1
  • 11
  • 2
    But it's a very bad idea to do so. Variable key names are just not good to have in a database. If anything the data should be store in an `Array`. ie: `rates: [{ user: Schema.Types.ObjectId, votation: Number }]` and that would store like: `rates: [{ user: ObjectId("5c981a0f8a76d426dc04619e"), votation: 1 },{ "user": ObjectId("5ca01bc41a97dd8b468b3f54"), "votation": 2 }]`. This is much easier for queries as indexes match on "values" and not "keys", as well as a number of purposes. Databases don't have the same rules as an array vs object in client code. – Neil Lunn Mar 31 '19 at 01:47
  • @NeilLunn , Thank you for your advice, so, now I modify my code and I have my data stored in this way: `"rates": [ { "user": "5ca01d2c56a2d9165c848f4f", "votation": 1 }, { "user": "5ca01d2c56a2d9165c821Df", "votation": 1 } ]`, Is this the best way to save this kind of information? Thank you. – Machiaveli Mar 31 '19 at 01:55
  • Possible duplicate of [Modelling blogs and ratings in mongodb and nodejs](https://stackoverflow.com/questions/10647969/modelling-blogs-and-ratings-in-mongodb-and-nodejs) – lifeisfoo Mar 31 '19 at 09:04

1 Answers1

0

If you need to store key/value properties related to the main document and then just visualize them, probably you need mongoose maps.

A map's keys are always strings.

rates: { type: Map, of: Number }

In this way you can have something like this:

rates: {"5c981a0f8a76d426dc04619e": 1}

Using this schema you can have multiple rates, one for each user/key.

But, as noted in the comments, this could be a sub optimal solution if need to do queries against these keys/values. In this case, an external schema reference should be used. In this case I'd use a three schema approach: Post, User and Rates.

  • a Post has multiple rates
  • a Rate belongs to the User and Post couple

I found a similar question already asked on SO.

lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
  • 2
    You can, but it's really bad practice to do so. Databases do things "fast" via "indexes", which are basically their own form of "key based lookups". MongoDB in particular is happiest matching the "value" rather than the "key". See the comment on the question. – Neil Lunn Mar 31 '19 at 01:49