0

I want to take 2n values from the database. In the below example I need to 2 points increment value in the mongodb database collection

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }

The output i am expecting is like this

 { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
..............
  • 1
    Have you tried anything? – Alex Blex Mar 07 '18 at 14:10
  • It's very hard to understand the exact behaviour you're expecting. From the title it seems you'd like to filter on even dates, but I can't find a pattern between your input and your output linked to the date. Are you just looking at even _id values? – Xavier Guihot Mar 07 '18 at 14:48
  • Yes even id values or values which we prefer. You are right @xaiver Guihot –  Mar 07 '18 at 15:05

1 Answers1

1

You can use the $mod (modulo) operator with a find query:

db.collection.find({ _id: { $mod: [2, 0]} })

Or (as mentioned in the title of your question) with an aggregate query:

db.collection.aggregate([{ $match: { _id: { $mod: [2, 0] } } }])
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190