0

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"]
    },
}
Ashkan S
  • 10,464
  • 6
  • 51
  • 80
  • Duplicate of [MongoDB: How to update multiple documents with a single command?](https://stackoverflow.com/questions/1740023/mongodb-how-to-update-multiple-documents-with-a-single-command) – Neil Lunn Nov 09 '17 at 11:43
  • @NeilLunn It is not a duplicate of that question. I even wrote the command that I expect to work based on MongoDB 3.4 documentation. My question is if I am designing my system correctly and if this way of doing it is too expensive when I will have a huge amount of data. – Ashkan S Nov 09 '17 at 11:48

1 Answers1

1

"Expensive" is a relative term, and also depends on other things, such as:

  • The provisioned hardware (CPU, RAM, etc.)
  • Your deployment topology (standalone, replica set, or sharded cluster)
  • How large the query will be (how many terms in the $in and the $addToSet)
  • Other factors

Unfortunately there is no single, easy answer to these questions, as many of them are very workload-dependent. Also, the query you posted is only one out of surely many others that you need.

Having said that, slow queries in MongoDB are typically caused by:

  • Underprovisioned hardware
  • Sub-optimal indexes (too few indexes, too many indexes, indexes don't have enough coverage, etc.)
  • Sub-optimal schema design for the workload

The only way to know if your planned queries are too expensive or not is to check out their explain() output, and see if there's any obvious improvements that can be made, such as the existence of COLLSCAN stages, small number of returned documents compared to examined documents (which means that the query is not targeted enough).

You may find these links usually contain enough information about optimization:

Once the queries are optimized, then it's time to put a simulated workload on the app & database, and monitor its performance using tools such as:

and confirm that you provisioned enough hardware for the workload you're expecting to see.

kevinadi
  • 13,365
  • 3
  • 33
  • 49