It is my first MongoDB project am still in the design phase of my System so I want to know if I am designing it right.
I want to know if it is possible to use addToSet method with $in selector and if it is, how expensive is that?
For example, I have some data like this:
{
{
Id:1,
Groups: ["Group1", "Group2"],
Events:["event1"]
},
{
Id:2,
Groups: ["Group2" ],
Events:["event2"]
},
,
{
Id:3,
Groups: ["Group1","Group3" ],
Events:["event1"]
},
}
Is it possible to call it like below and how expensive is that?
db.test.update(
{ Groups: { $in: ["Group1"] } },
{ $addToSet: {Events: [ "newEvent1", "newEvent2" ] } }
)
- P.S: I would assume that in short time I will end up with millions of records and in the worst case scenario 1 million rows can belong to a group.
*P.S.S: Since the query is on a list property, should I add an index or something? Is it even a correct way to go?
Update
Maybe I am using the wrong selector, but I just want to update the rows that have the group1 in their groups. So the results expected to be like this:
{
{
Id:1,
Groups: ["Group1", "Group2"],
Events:["event1", "newEvent1", "newEvent2"]
},
{
Id:2,
Groups: ["Group2" ],
Events:["event2"]
},
,
{
Id:3,
Groups: ["Group1","Group3" ],
Events:["event1","newEvent1", "newEvent2"]
},
}