This is a matter of document design. Roughly, there are two routes to follow:
Include the subdocument ( artigo
) within the root document ( usuario
)
link / reference secondary document ( artigo
) in primary ( usuario
)
As you seem to need an identifier for each article, I would first suggest trying to find a natural identifier, such as a single existing archival number. If this is not possible, you can generate any unique identifier at the time of saving the article for the first time, type a UUID / GUID or some string derived from the title and possibly publication date (eg id_artigo: "2014_08_03_lorem_imsum"
).
If you prefer a sequential number for these articles, a simple way to do this is by creating a sequencias
auxiliary collection that you can use to generate those numbers. I use this technique a lot with findAndModify()
:
var contador = db.sequencias.findAndModify({
query: {'_id': 'artigos'},
fields: {'_id': 0, 'seq': 1},
update: {'$inc': {'seq': 1}},
new: true,
upsert: true
});
var novo_num = contador.seq;
The 1 alternative applies well when articles have only one author / user because you will not duplicate anything:
{
_id: ObjectId("123456..."),
user: "João",
artigos:[
{
id_artigo: "2014_08_03_lorem_impsum",
data: new Date(2014, 07, 03),
titulo: "Lorem ispsum.",
texto: "Lorem ipsum dolor sit amet..."
},
{
id_artigo: "2013_12_01_so_um_teste",
data: new Date(2013, 11, 01),
titulo: "Só um Teste",
texto: "Meu primeiro artigo..."
}
]
}
Alternatively 2 can be used when there is a possibility that the same article can be related to more than one author / user, avoiding too much duplication of data:
Articles :
{
_id: "2014_08_03_lorem_impsum",
data: new Date(2014, 07, 03),
titulo: "Lorem ispsum.",
texto: "Lorem ipsum dolor sit amet..."
}
{
_id: "2013_12_01_so_um_teste",
data: new Date(2013, 11, 01),
titulo: "Só um Teste",
texto: "Meu primeiro artigo..."
}
Users :
{
_id: ObjectId("123456..."),
user: "João",
artigos: ["2013_12_01_so_um_teste", "2014_08_03_lorem_impsum"]
}
{
_id: ObjectId("987654..."),
user: "Armando",
artigos: ["2014_08_03_lorem_impsum"]
}