These are chat archives for Automattic/mongoose

23rd
Jun 2017
Mihail
@metronom72
Jun 23 2017 05:43

@benydc your syntzx is incorrect. Wrong code is near

{
  "_id": idvalue,
  "author":{
    "firstname":"Daniel",
    "lastname": "Defoe"
  },
  "books":[
    {
      "2234"
      "1714"
    }
  ]
}

should be like this, as i understand

{
  "_id": idvalue,
  "author":{
    "firstname":"Daniel",
    "lastname": "Defoe"
  },
  "books":[
    {
      2234: "2234",
      1714: "1714"
    }
  ]
}
and looking by it will be like this collection.find({"books.2234": 2234})
romie
@theromie
Jun 23 2017 06:45
I am new to mongoose and i want to know like we do in mysql, We separate each table with referencing to that table id so it is good to same thing in mongoose or save all data into one table?
Mihail
@metronom72
Jun 23 2017 06:51
@theromie mongoose is not a relation database, it's flat database. It will be slower then any relation if you will think as key references. It have those possibility, by will be slower.
If you need reusable data then you should use relation database (any SQL).
In another way, you should use nested documents, smth like this:
{
  foo: 'bar',
  child: {
    bar: 'baz',
    children: [
      {
        barbaz: 1,
        bazbar: 'asdfasdf',
        isActive: true,
      }
    ]
  }
}
in relation model you will have 3 tables, with one one to one and one one to many references.
Julio Borja Barra
@juboba
Jun 23 2017 09:58
hey guys, is it possible to find by child attribute? i.e.
I have group that has many categories. I would like to find every group where one of the categories has some category.name
categories lives in another collection
romie
@theromie
Jun 23 2017 10:02
@metronom72 so according to you store all data into one table rather than using relations
Mihail
@metronom72
Jun 23 2017 10:13

yes. mongo doesn't have tables it have document, if you want to reuse document in another you should copy document.

If you want references, you can do smth like

{
  _id: ObjectId('asdfasdfasdfasdfasdf'),
  name: 'my awesome document',
  children: [
    oneObjectId,
    anotherObjectId,
    notExistedId,
    ...,
  ],
}
{
  _id: oneObjectId,
  name: 'asdfasdfa',
}
{
  _id: anotherObjectId,
  name: 'asdfasdfasdf',
}

But you should check existence of object ids in children manually, and it will be slower, and almost it's bad practicing))