0

I'm trying to add a field, "num", in my MongoDB collection such that each object has an incremented number.

{num: 1}
{num: 2}
{num: 3}
manwitha
  • 37
  • 6
  • You mean you want to update existing collection or implement general mechanism of auto-incremented field ? – mickl May 08 '18 at 15:59
  • I'd like to update an existing collection by adding the number field. I'm using an aggregate method combined with $match, $project, and $group, but I need to add another field. – manwitha May 08 '18 at 16:15
  • The accepted answer here actually proposes to "stuff the entire collection" into a single BSON document. In the "real world" this is not possible due to the [16MB BSON Limit](https://docs.mongodb.com/manual/reference/limits/). Whilst you may get away with that for very small data, most people don't have a collection which would fall under 16MB. And even when they do as yourself, then you should simply loop the items in the collection and write the additional data. If the collection is small enough to fit in a single BSON document then it will not matter to you anyway as a "one second" update – Neil Lunn May 08 '18 at 20:48
  • @NeilLunn that's not true. https://docs.mongodb.com/manual/core/aggregation-pipeline-limits/ "The limit only applies to the returned documents; during the pipeline processing, the documents may exceed this size." – mickl May 08 '18 at 21:01
  • @mickl You're incorrect here `{ "$group": { "_id": null` produces a **single** BSON document. You don't seem to understand that and seem to think we are talking about "cursors" and "response". That's not the issue, but the "single document" is. Please don't reference me to documentation, as it's clearly you who needs to be reading it and understanding it. Hence the note. – Neil Lunn May 08 '18 at 21:03
  • @NeilLunn so I don't get that. Could you explain what does it mean "during processing" and when can I exceed 16 MB in Aggregation Framework ? – mickl May 08 '18 at 21:10
  • @mickl Little excercise for yourself. Create 18 documents and pad a field with 1MB of data, then try the `$group` with a `null` key and see what happens. This is written over and over again in questions and answers here ( mostly on `$lookup` of late ) where you cannot put that amount of content into a single document. Even the answer on one of the marked duplicates has the sense to warn the reader that this is the case. – Neil Lunn May 08 '18 at 21:17
  • @NeilLunn good idea, I did that. Using MongoShell (which is fine since he only needs one time update) and v 3.6. I have 1k large documents. When I use $group with null and $push I'm getting "BSONObj size: 19490908 (0x129685C) is invalid. Size must be between 0 and 16793600(16MB)" which is what we both expect. But when I add $unwind and all the other stages from my answer (including $out) it works fine. Again, I might be wrong, I can even open another question related to this but it looks like it's working exactly the way it is described in the docs. What am I missing here ? – mickl May 09 '18 at 05:49
  • @mickl You are actually wrong, so it's not a matter of "could be". You can see the basic premise [demonstrated here](https://stackoverflow.com/a/45726501/2313887). That's an array from a `$lookup` target but it's the same principle as pushing to an array. The limit gets broken before the `$unwind` can be reached and it's really reproducible, which makes the answer incorrect for usage in the real world. So trying to tell people it works when it does not is simply a false statement, misleading and dangerous. – Neil Lunn May 09 '18 at 06:48

0 Answers0