0

I'm sure this is the easiest question for you, but forgive me for being naive. I have been trying to learn mongodb and wanted to experiment with some aggregation.

For that, I wanted to insert data into a collection like this.

for (i=1; i<=10; i++) { db.aggtest.insert( {a : { $mod: [i, 3]}, b : i}) }

However this doesn't seem to work. I have tried using it for update as well, but it gives a different error for me.

Please help me with this.

Thanks Radha

Radha
  • 15
  • 4
  • Either you wrote your insert incorrectly on this question, or you have an extra '}' after '[i, 3]' – Jon Dec 22 '16 at 17:30
  • Yes forgive me, its incorrect in the question, but it doesn't even if I have the correct ones. – Radha Dec 22 '16 at 17:32
  • This is what I have tried for (i=1; i<=10; i++) { db.aggtest.insert({ a: { $mod : [i, 3] }, b : i}) } – Radha Dec 22 '16 at 17:33
  • Please edit your question to reflect that, otherwise you might get responses that don't answer your question. Also, what errors are you getting? – Jon Dec 22 '16 at 17:34
  • Thanks, this is the error I get. – Radha Dec 22 '16 at 17:39
  • Error: field names cannot start with $ [$mod] :... Edited my original post as well, by the way – Radha Dec 22 '16 at 17:39

1 Answers1

0

The $mod operator is used for queries and projection.

https://docs.mongodb.com/manual/reference/operator/query/mod/

For your javascript function use the language specific operator for insertion:

for (i=1; i<=10; i++) { db.aggtest.insert( {a : i%3, b : i}) }
Jai Hirsch
  • 141
  • 2